What I'm doing
In some part of the code, I have a listener on focusin
events, and in another part programmatically set the focus on an input
. On Chrome, Safari, Firefox the event listener is called once, but on IE (including IE10), it is called twice. I register the listener with jQuery's .on()
and set the focus with jQuery's .focus()
. See below for the full source of an example that shows this behavior, and if you wish, you can run that example.
Questions
- Even when not using jQuery, IE is firing
focusin
twice. And it does so only when the focus is set programmatically, not when users tab or click on the field. Why? Is it just an IE bug, or does IE have a good reason for behaving this way? - Whether it is a IE bug or not, shouldn't jQuery iron out the difference between IE and other browsers here? In other words, is not doing so a jQuery bug?
- How would you work around this? (I.e. so I can have code that runs just once per focus, whether the focus is set programmatically or by users.)
Full source
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
$(function() {
$('input').on('focusin', function() {
var c = $('#count');
$('#count').text(1 + parseInt(c.text()));
console.log('focusin');
});
$('input').focus();
});
</script>
</head>
<body>
<input>
<code>focusin</code> received: <span id="count">0</span>.
</body>
</html>