I have tried numerous things and nothing seems to be working.
I am using jQuery and Chosen plugin.
Methods I have tried:
var select = jQuery('#autoship_option');
select.val(jQuery('options:first', select).val());
jQuery('#autoship_option').val('');
jQuery('#autoship_option').text('');
jQuery('#autoship_option').empty('');
jQuery("#autoship_option option[value='']").attr('selected', true);
It always shows the Active Autoship option once it has been selected. I can't seem to get it to clear the selection.
Here is the select box:
<select id="autoship_option" data-placeholder="Choose Option..."
style="width: 175px;" class="chzn-select">
<option value=""></option>
<option value="active">Active Autoship</option>
</select>
Anyone familiar with Chosen and being able to clear a select box with one option? (It will have more options in the future.
Setting the
select
element's value to an empty string is the correct way to do it. However, that only updates the rootselect
element. The customchosen
element has no idea that the rootselect
has been updated.In order to notify
chosen
that theselect
has been modified, you have to triggerchosen:updated
:or, if you're not sure that the first option is an empty string, use this:
Read the documentation here (find the section titled Updating Chosen Dynamically).
P.S. Older versions of Chosen use a slightly different event:
Use above config and apply
class='chosen-select-deselect'
for manually deselect and set the value emptyor if you want deselect option in all combo then apply config like below
and set the default option value to
''
.It has to be used with chosen updated jQuery available at this link: https://raw.github.com/harvesthq/chosen/master/chosen/chosen.jquery.min.js.
I spent one full day to find out at the end that
jquery.min
is different fromchosen.jquery.min
If you need to have first option selected by no matter of its value (sometimes first option value is not empty) use this:
It will get value of first option and set it as selected then trigger update on Chosen. So it work like reset to first option on list.
Simple add trigger change like this:
If you are using
chosen
, a jquery plugin, then to refresh or clear the content of the dropdown use:chosen:updated
event willre-build
itself based on the updated content.