I am handling two events, both focusout
and keydown
, in a $(elementID).on()
event-handler.
Calling $(elementID).off("focusout keydown");
as the last line within the .on()
event-handler seems to be partially working -- focusout
is working correctly, but keydown
is firing twice.
Edit: With
@Barmar
's findings, keydown
is first triggering focusout
and then keydown
. This is happening in Firefox 22, but apparently not in Chrome 29.
Here is the HTML:
<input type = "text" id = "textField" />
<input type = "button" onclick = "setFocus();" value = "click here" />
<ol>
<li>Type some text into the textfield.</li>
<li>Click the button.</li>
<li>
Click out of the textfield, or
<br />
<i>press enter for some weird behavior.</i>
</li>
</ol>
...here is the javascript / jQuery:
function setFocus() {
$("#textField").focus();
$("#textField").select();
var count = 1;
$("#textField").on("focusout keydown", function(e) {
// if clicked away, or the enter key is pressed:
if (e.type == "focusout" || e.keyCode == 13) {
alert(e.type + ": " + count++);
}
// this doesn't seem to be working correctly:
$("#textField").off("focusout keydown");
});
}
...and here is the jsFiddle.