Skip to content

Event: Don't break focus triggering after .on(focus).off(focus) #4885

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 10, 2021

Conversation

mgol
Copy link
Member

@mgol mgol commented May 7, 2021

Summary

The _default function in the special event settings for focus/blur has
always returned true since gh-4813 as the event was already being fired
from leverageNative. However, that only works if there's an active handler
on that element; this made a quick consecutive call:

elem.on( "focus", function() {} ).off( "focus" );

make subsequent .trigger( "focus" ) calls to not do any triggering.

The solution, already used in a similar _default method for the click event,
is to check for the dataPriv entry on the element for the focus event
(similarly for blur).

Fixes gh-4867

+10 bytes

Checklist

Sorry, something went wrong.

The `_default` function in the special event settings for focus/blur has
always returned `true` since jquerygh-4813 as the event was already being fired
from `leverageNative`. However, that only works if there's an active handler
on that element; this made a quick consecutive call:

```js
elem.on( "focus", function() {} ).off( "focus" );
```

make subsequent `.trigger( "focus" )` calls to not do any triggering.

The solution, already used in a similar `_default` method for the `click` event,
is to check for the `dataPriv` entry on the element for the focus event
(similarly for blur).

Fixes jquerygh-4867
Copy link
Member

@timmywil timmywil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You make it look so simple.

@mgol mgol removed the Needs review label May 10, 2021
@mgol mgol merged commit e539bac into jquery:main May 10, 2021
@mgol mgol deleted the focus-on-off-fix branch May 10, 2021 16:59
mgol added a commit to mgol/jquery that referenced this pull request May 10, 2021
The `_default` function in the special event settings for focus/blur has
always returned `true` since jquerygh-4813 as the event was already being fired
from `leverageNative`. However, that only works if there's an active handler
on that element; this made a quick consecutive call:

```js
elem.on( "focus", function() {} ).off( "focus" );
```

make subsequent `.trigger( "focus" )` calls to not do any triggering.

The solution, already used in a similar `_default` method for the `click` event,
is to check for the `dataPriv` entry on the element for the focus event
(similarly for blur).

Fixes jquerygh-4867
Closes jquerygh-4885

(cherry picked from commit e539bac)
@mgol
Copy link
Member Author

mgol commented May 10, 2021

Landed on main in e539bac & on 3.x-stable in b3e4a7e.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

You cannot programmatically focus an element after removing its focus handler
2 participants