Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery .change() event is triggered with .click() in newer versions?
    primarykey
    data
    text
    <p>I'm currently upgrading my application to use jQuery 1.6.1 (previously using 1.4.4) and found that now the <code>.click()</code> event automatically triggers a <code>.change()</code> event as well.</p> <p>I created a simple example here: <a href="http://jsfiddle.net/wDKPN/" rel="noreferrer">http://jsfiddle.net/wDKPN/</a></p> <p>Notice if you include 1.4.4 the <code>.change()</code> function will <strong>not</strong> fire when the <code>.click()</code> event is triggered. But when switching to 1.6, the <code>.change()</code> event <strong>is</strong> fired when <code>.click()</code> is triggered.</p> <p>Two questions:</p> <ol> <li><p><strong>Is this a bug?</strong> It seems that programmatically triggering <code>.click()</code> <em>shouldn't</em> also fire other events (for example, it would seem wrong to also automatically fire <code>.blur()</code> and <code>.focus()</code>, to help "mimic" a user's click).</p></li> <li><p><strong>What is the proper way for me to bind a <code>change()</code> event <em>and then</em> trigger <em>both</em> a <code>click()</code> and <code>change()</code> event for that element?</strong> Do I simply call <code>.click()</code>, and rely on the fact that <code>.change()</code> will also fire?</p> <pre><code>$('#myelement').change(function() { // do some stuff }); $('#myelement').click(); // both click and change will fire, yay! </code></pre></li> </ol> <p>In my old code I'm using this pattern to initialize some checkboxes (and their checked states and values) after an ajax call:</p> <pre><code> $('#myelement').change(function() { // do some stuff including ajax work }).click().change(); </code></pre> <p>But in 1.6.1 my logic fires twice (once for <code>.click()</code> and once for <code>.change()</code>). Can I rely on just removing the <code>.change()</code> trigger and hope that jQuery continues to behave this way in future versions?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COEven though this is a very good observation, I would call it more of a 'fix' than a 'bug'. Clicking the checkbox makes it change state, hence there is a completely valid reason for it to trigger change event as a consequence. To me it's strange that it wasn't doing that before (and there was [a thread on SO](http://stackoverflow.com/questions/2161213/why-is-onchange-on-a-checkbox-not-fired-when-the-checkbox-is-changed-indirectly) for that matter).
      singulars
    2. COCertainly, I think I can agree this was on purpose and probably not a bug. I hope it stays this way for upgrades though... And can't we argue then for other events? For example, can we rephrase this as saying, "If a user **clicks** a checkbox they **must** have changed it too". Can we also say, "If a user **clicks** a checkbox it **must** have received focus". But the .focus() event doesn't fire automatically with .click() the way .change() does.
      singulars
    3. COinteresting facts, did yopu made some tests on IE (6?) as I remember some strange behaviors with checkboxes/jQuery and event chain? Did you check as well with triggerHandler('click'), as the doc said "The .triggerHandler() method does not cause the default behavior of an event to occur (such as a form submission)", so does this make the chained change run now?
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload