I'm currently upgrading my application to use jQuery 1.6.1 (previously using 1.4.4) and found that now the .click()
event automatically triggers a .change()
event as well.
I created a simple example here: http://jsfiddle.net/wDKPN/
Notice if you include 1.4.4 the .change()
function will not fire when the .click()
event is triggered. But when switching to 1.6, the .change()
event is fired when .click()
is triggered.
Two questions:
Is this a bug? It seems that programmatically triggering
.click()
shouldn't also fire other events (for example, it would seem wrong to also automatically fire.blur()
and.focus()
, to help "mimic" a user's click).What is the proper way for me to bind a
change()
event and then trigger both aclick()
andchange()
event for that element? Do I simply call.click()
, and rely on the fact that.change()
will also fire?$('#myelement').change(function() { // do some stuff }); $('#myelement').click(); // both click and change will fire, yay!
In my old code I'm using this pattern to initialize some checkboxes (and their checked states and values) after an ajax call:
$('#myelement').change(function() {
// do some stuff including ajax work
}).click().change();
But in 1.6.1 my logic fires twice (once for .click()
and once for .change()
). Can I rely on just removing the .change()
trigger and hope that jQuery continues to behave this way in future versions?