Skip to content

You cannot programmatically focus an element after you have attempted to focus it while it was hidden (display:none) #4950

Closed

Description

@JoolsCaesar

Description

Possibly related to #4867, I have replicated this in version 3.5.1 and 3.6. The linked example below uses 3.6. I haven't tested Firefox, but both Chrome and Edge display this issue.

See the following example. There's a focusable div at the top, which has a handler to report (at the bottom of the example) the number of times it is focused. When you click the div, it is only ever focused programmatically, because a handler calls preventDefault() on the mousedown.

There is a button to toggle the visibility (display) of the focusable element, but before showing the element, it attempts to programmatically focus it.

Focusing a display:none element is not expected to work, but the issue is what follows.

Once the element is shown, no future attempt to focus the element will work.

HTML:

  <div id="hideable">
    <div id="focusable" tabindex="1">
      I am focusable
    </div>
  </div>
  <button id="button">
    Toggle visibility
  </button>
  <div id="output">
    Click "I am focusable" to focus. Click "Toggle visibility" to hide/show the focusable element
  </div>

JS:

const $button = $('#button');
const $focusable = $('#focusable');
const $hideable = $('#hideable');
const $output = $('#output');

let focusinCount = 0;
$hideable.on('focusin', function() {
  focusinCount++;
  $output.text('I felt that ' + focusinCount);
});

let visible = true;
$button.click(function() {
  if (visible) {
    $hideable.hide();
  } else {
    $focusable.focus(); // Comment out this to fix
    $hideable.show();
  }
  visible = !visible;
});

$focusable.on('mousedown', function(event) {
  event.preventDefault();
  $focusable.focus();
})

Link to test case

https://jsfiddle.net/JoolsCaesar/2o089jga/

Activity

  1. mgol commented on Oct 11, 2021

    @mgol
    Member

    Thanks for the report. This looks like a valid issue.

  2. added this to the 3.6.1 milestone on Oct 11, 2021
  3. added
    Discuss in MeetingReserved for Issues and PRs that anyone would like to discuss in the weekly meeting.
    on Oct 11, 2021
  4. removed
    Discuss in MeetingReserved for Issues and PRs that anyone would like to discuss in the weekly meeting.
    on Oct 18, 2021
  5. jwmerrill commented on Nov 10, 2021

    @jwmerrill

    I have bisected this problem to fe5f04d using the provided test case.

  6. jwmerrill commented on Nov 10, 2021

    @jwmerrill

    It appears that commenting out this line

    event.stopPropagation();

    fixes the test case, but presumably that would cause other problems.

  7. fullermetric commented on Mar 10, 2022

    @fullermetric

    Thanks for this Joels.
    The issue became more apparent after adding #focusable:focus { outline: 3px solid green; } to your test case.

    Came here in an attempt to resolve some instances where using .focus() generates a keyup code of 13 (version 3.3.1)
    In an attempt to resolve keycode generating on .focus I switched out 3.3.1 and tried jQuery versions 3.5 and 3.6 but found that .focus was not working at all ( as you pointed our why in your test case).

    In the end, I switched back to 3.31 and now using .focus inside of a timeout

    $(".tabs-stage #tab-1").addClass('hide'); $(".tabs-stage #tab-2").removeClass('hide'); $(".tabs-stage #tab-3").addClass('hide'); //fmp.triggeredOrdQtyFocus = 1; //$("#product-1").find("#product-qty").focus(); setTimeout(function(){ $("#product-1").find("#product-qty").focus(); },200);

  8. deleted a comment from on Apr 11, 2022
  9. deleted a comment from on Apr 11, 2022
  10. edg2s commented on Jun 13, 2022

    @edg2s
    Contributor

    We discovered this same issue at Wikipedia (upstream issue: https://phabricator.wikimedia.org/T285626, minimal codepen: https://codepen.io/edg2s/pen/eYVbLOj)

    This occurs in one dialog that we know of, but the nature of our dialogs means they are regularly hiding and showing content, and moving user focus, so accidentally focusing a hidden input is probably happening elsewhere.

  11. 11 remaining items

  12. added 8 commits that reference this issue on Mar 17, 2023
    f5bdeac
    7022908
    bd4397c
    9952fdc
    5addd41
    f5b7dc0
    71e6bbe
    fd2653b
  13. mgol commented on Mar 27, 2023

    @mgol
    Member

    Fixed on main (future 4.x) via #5223 and on 3.x-stable via #5224.

  14. 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

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions