Skip to content

You cannot programmatically focus an element after removing its focus handler #4867

Closed

Description

@JoolsCaesar

Description

I'm seeing this in the latest chromium browsers Windows Chrome & Edge 89, using jquery 3.6.

If you add and remove an element's focus handler, subsequent calls to $element.focus(), will do nothing.
HTML:

<div tabindex="-1" class="fdiv"></div>

CSS:

.fdiv{
  height:100px;
  width:100px;
  border:1px solid black;
}
.fdiv:focus{
  background-color:green;
}

See the linked to test case below. I have a simple div that turns green when you focus it and some js that will focus said div after 2 seconds.
Before setting this timeout I add and then immediately remove an empty focus handler:

//Using https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js

const $origDiv = $('.fdiv');

// Comment out the following line to fix
$origDiv.on('focus',function(){}).off('focus');

setTimeout(function(){
	$origDiv.focus();
},2000)

If you run the fiddle, the div should turn green (gain focus) after 2 seconds, but it doesn't!
If you comment out the focus handler line, all works as expected.

Link to test case

https://jsfiddle.net/JoolsCaesar/ut371acd/


EDIT by @mgol: I fixed syntax highlighting in code blocks.

Activity

  1. JoolsCaesar commented on Mar 29, 2021

    @JoolsCaesar
    Author

    FWIW I tracked this down to this bit in jquery:

    function leverageNative( el, type, expectSync ) {
    
    	// Missing expectSync indicates a trigger call, which must force setup through jQuery.event.add
    	if ( !expectSync ) {
    		if ( dataPriv.get( el, type ) === undefined ) {
    			jQuery.event.add( el, type, returnTrue );
    		}
    		return;
    	}

    dataPriv.get( el, type ), where el is the element we're attempting to focus and type is 'focus', returns false, so we just drop out without doing anything.

  2. added this to the 3.6.1 milestone on Mar 29, 2021
  3. self-assigned this
    on Mar 29, 2021
  4. added
    Discuss in MeetingReserved for Issues and PRs that anyone would like to discuss in the weekly meeting.
    on Mar 29, 2021
  5. mgol commented on Mar 29, 2021

    @mgol
    Member

    Thanks for the report! The code you mentioned exists since jQuery 3.4.0, I suspect it only became a problem after #4813 landed.

  6. JoolsCaesar commented on Mar 29, 2021

    @JoolsCaesar
    Author

    Makes sense. I spotted this after upgrading from 3.0.0 to 3.6.0, so couldn't narrow it down much.

  7. mgol commented on Mar 29, 2021

    @mgol
    Member

    I meant the bug is only reproducible in jQuery 3.6.0, it works fine in 3.5.1 despite the fact 3.5.1 contains the code you linked to. Hence, most likely it only causes the issue in connection with #4813 (although I haven't verified it yet).

  8. removed
    Discuss in MeetingReserved for Issues and PRs that anyone would like to discuss in the weekly meeting.
    on Apr 26, 2021
  9. added a commit that references this issue on May 7, 2021
    36edef1
  10. mgol commented on May 7, 2021

    @mgol
    Member

    PR: #4885

  11. added 2 commits that reference this issue on May 10, 2021
    e539bac
    b3e4a7e
  12. locked as resolved and limited conversation to collaborators on Sep 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions