You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
You cannot programmatically focus an element after you have attempted to focus it while it was hidden (display:none) #4950
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();
})
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
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.
Activity
mgol commentedon Oct 11, 2021
Thanks for the report. This looks like a valid issue.
jwmerrill commentedon Nov 10, 2021
I have bisected this problem to fe5f04d using the provided test case.
jwmerrill commentedon Nov 10, 2021
It appears that commenting out this line
jquery/src/event.js
Line 577 in 4250b62
fixes the test case, but presumably that would cause other problems.
Use native focus events insted of jQuery focus events
Use native focus events insted of jQuery focus events
fullermetric commentedon Mar 10, 2022
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);Bug 30640: Focus does not always move to correct search header form f…
edg2s commentedon Jun 13, 2022
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 remaining items
Event: Simulate focus/blur in IE via focusin/focusout
Tests: Add a test for jquerygh-4950
Event: Simulate focus/blur in IE via focusin/focusout
Tests: Add a test for jquerygh-4950
Event: Simulate focus/blur in IE via focusin/focusout
Tests: Add a test for jquerygh-4950
Event: Simulate focus/blur in IE via focusin/focusout
Tests: Add a test for jquerygh-4950
Event: Simulate focus/blur in IE via focusin/focusout
Event: Simulate focus/blur in IE via focusin/focusout (3.x version)
mgol commentedon Mar 27, 2023
Fixed on
main(future4.x) via #5223 and on3.x-stablevia #5224.fixes issue where jquery can't focus on the select2 search box, may b…
Use native focus events insted of jQuery focus events