Consider this example where I have 2 input fields:
<input id="a" />
<input id="b" style="display: none" />
And consider the following JavaScript, which is an attempt to do this:
Show #b
only when #a
has focus and hide #b
whenever #a
loses focus, except when #a
loses its focus to #b
.
$("#a").focus(function() {
$("#b").show();
});
$("#a, #b").blur(function() {
$("#b").hide();
});
$("#b").focus(function(){
$("#b").show();
});
$("#a").focus(function() {
$("#b").show();
});
$("#a, #b").blur(function() {
$("#b").hide();
});
$("#b").focus(function() {
$("#b").show();
});
#b {
display: none;
}
<input id="a" value=a>
<input id="b" value=b>
<br/>^ focus on the input
The above code is incorrect as $("#b").focus()
would never be triggered because as soon as #a
loses focus, #b
is hidden. This expected behavior is observed in Firefox (Version 24.6.0).
But in Chrome (Version 35.0), the code seems to run incorrectly (or correctly!?).
Clearly, the b.focus
event is still being registered in Chrome.
Why does this event register in Chrome, but not in Firefox?
Update
As pointed out by raina77ow:
- In Chrome, after we place the cursor on
b
, blur ona
is fired first, then focus onb
, andb
stays visible. - In Firefox, focus on
b
is not fired, sob
becomes invisible. - In IE10, however, somehow focus on
b
IS fired, butb
becomes invisible immediately, as blur is fired onb
right after.
Here's a fiddle without using jQuery, producing the same behavior.