Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery Chosen not firing onchange event when value is changed through JS
    primarykey
    data
    text
    <h1><a href="http://jsfiddle.net/ult_combo/y6BCP/" rel="nofollow noreferrer"><strong>JSFiddle</strong></a></h1> <p>I've set up a <code>select</code> with 3 <code>option</code>s (1 blank) in the fiddle. When I switch from <code>foo</code> to <code>bar</code> manually, the <code>change</code> listener fires normally.</p> <p>Now if I reset the <code>select</code> through JS with the <a href="https://stackoverflow.com/questions/7897760/jquery-chosen-reset">code posted in some answers here</a>, the <code>change</code> event does not fire for the newly selected blank option, furthermore, if I select the option which was selected before clicking in the <code>reset</code> button the <code>onchange</code> event won't fire either.</p> <p>I'm sure the <code>select</code>'s value <strong>is</strong> being changed with the function above, which can be seen in this <a href="http://jsfiddle.net/ult_combo/y6BCP/1/" rel="nofollow noreferrer"><strong>fiddle</strong></a>, but the <code>select</code>'s <code>onchange</code> event simply doesn't fire.</p> <p>I've also tried <code>onchange</code>, <code>onclick</code>, <code>oninput</code> events to no avail. Right now I'm wondering if I should use a MutationObserver or remove the rendered chosen element from the DOM and create another, but I'm probably overthinking it. Any help is appreciated.</p> <p>Also:</p> <p>This answer helped me a lot: <a href="https://stackoverflow.com/questions/7897760/jquery-chosen-reset">jQuery Chosen reset</a></p> <p>This answer didn't help: <a href="https://stackoverflow.com/questions/8455569/how-to-fire-onchange-event-in-chosen-prototype-javascript-select-box">how to fire onchange event in chosen prototype javascript select box?</a></p> <p>Code for future reference if jsfiddle.net is taken down:</p> <p><strong>HTML</strong></p> <pre><code>&lt;div id="wrapper"&gt; &lt;select id="chosen"&gt; &lt;option value="!!EMPTY VALUE!!"&gt;&lt;/option&gt; &lt;option value="foo"&gt;foo&lt;/option&gt; &lt;option value="bar"&gt;bar&lt;/option&gt; &lt;/select&gt; &lt;/div&gt; &lt;br&gt; &lt;button id="reset"&gt;Reset&lt;/button&gt; </code></pre> <p><strong>JS</strong></p> <pre><code>$(function() { $('#chosen').chosen(); $('#wrapper').on('change', '#chosen', function() { console.log('onchange: ' + this.value); }); $('#reset').click(function() { $("#chosen").val('').trigger("liszt:updated"); //doesn't work //$("#chosen").prop('selectedIndex', 0).trigger("liszt:updated"); //doesn't work either console.log('reset: ' + $('option:selected').val()); }); }); </code></pre>
    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.
 

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