google chrome - jQuery change event fired twice when giving focus to another control inside the change callback -
a weird bug caused me lot of headaches recently, , i've been able dumb down simplest form possible. see fiddle : http://jsfiddle.net/pgaab/
<input type="text" id="foo" placeholder="change me!"><br> <input type="text" id="bar" size="30" placeholder="dummy control switch focus"> $('#foo').change(function() { console.log('changed!'); $('#bar').focus(); }); basically, when change first textbox , use mouse click elsewhere in document, change event fires, usual. however, if change value, , hit enter key trigger change, event fires twice.
i've noticed bug chrome. firefox not trigger event twice, , ie not support enter key trigger change on input.
i guess happens because of focus switching inside event callback. there way around this?
the focus() on other control in change eventhandler call change event in chrome because unfocus "blur" current control if value different.
this bug not new, can take @ bug ticket on jquery : http://bugs.jquery.com/ticket/9335
you can work around disabling change eventhandler before remove focus on control. here little exemple of want say:
$('#foo').change(changehandler); function changehandler() { console.log('changed!'); $(this).off('change').blur().on('change', changehandler); $('#bar').focus(); }
Comments
Post a Comment