How can I reset a form with jQuery chosen plugin?

JavascriptJqueryJquery PluginsJquery Chosen

Javascript Problem Overview


I have a bunch of select elements in a form with which I am using the Jquery Chosen plugin. How can I reset the form? The following does not work:

<input type="reset" />

Javascript Solutions


Solution 1 - Javascript

You'll need to reset the value of the field, then trigger the liszt:updated event on the input to get it to update, ive made a fiddle with a working example here.

http://jsfiddle.net/VSpa3/3/

$(".chzn-select").chosen();
$('a').click(function(){
    $(".chzn-select").val('').trigger("liszt:updated");
});​

Since the release of chosen v1.0 the trigger is now called 'chosen:updated'. Anyone using this new version needs to trigger the update using

$(".chosen-select").val('').trigger("chosen:updated");

Solution 2 - Javascript

Since the release of chosen v1.0 the trigger is now called 'chosen:updated'. Anyone using this new version needs to trigger the update using

$(".chosen-select").val('').trigger("chosen:updated");

Solution 3 - Javascript

You could try this:

$('select').chosen('destroy');  

$('select').prop("selectedIndex", -1);   
$('select').chosen();

Solution 4 - Javascript

in order to have reset working naturally use this:

$("input[type='reset'], button[type='reset']").click(function(e){
    e.preventDefault();
    
    var form = $(this).closest('form').get(0);
    form.reset();
    
    $(form).find('select').each(function(i, v) {
        $(v).trigger('chosen:updated');
    });
}

Solution 5 - Javascript

$("#Your_id").trigger("chosen:updated");

This worked for me

Solution 6 - Javascript

For a more hassle free approach...assuming your inputs are inside <form> tags:

<form>
    <!-- your selects go here and any other input fields -->
    <input type="reset" value="Reset"> 
</form>

This is what I would do:

$("input[type='reset'], button[type='reset']").click(function(e){
      setTimeout(function(){ $("select").trigger("chosen:updated"); }, 50);
});

See fiddle here.

Solution 7 - Javascript

None of the previous options work for me. I had to do it like the old school, even using some native javascript, here is the code:

$('#dllSample option').each(function(){
     $(this)[0].selected = false;   
});
$("#dllSample").trigger("chosen:updated");

Solution 8 - Javascript

None of the answers here worked for me. Im using chosen select with the multiple attribute. Normal submit form button didnt do the trick either. So i just had a function do this upon click.

//Gets the selected values
var selected = [];
    for (var option of document.getElementById('mySelect').options) {
        if (option.selected) {
            selected.push(option.value);
        }

    }

//Resets the form
document.getElementById('form1').reset();

//Chosen values doesnt get reset, so we do this manually
//Removes the values selected by clicking on the x icon
$link = $('a.search-choice-close');
            var i;
            for (i = 0; i < selected.length; i++) {
                $link[i].click();
            }

Solution 9 - Javascript

Sometimes you have to reset the select that chosen is called on.

I do this

jQuery.fn.chosen_reset = function(n){
  $(this).chosen('destroy');
  $(this).prop('selectedIndex', 0);
  $(this).chosen(n)
}

And call this function like this, with the options as an argument

$('select').chosen_reset({width:'369px'});

Solution 10 - Javascript

In jQuery something like this should work

<input name="Text1" id="something" type="text">

<input type="reset" id="reset_me"/>


$("#reset_me").click(function() { 
$("#something").val("");
});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionP HView Question on Stackoverflow
Solution 1 - JavascriptsottenadView Answer on Stackoverflow
Solution 2 - JavascriptJack O'NeillView Answer on Stackoverflow
Solution 3 - JavascriptEdgar OView Answer on Stackoverflow
Solution 4 - JavascriptbuzdykgView Answer on Stackoverflow
Solution 5 - JavascriptRajesh KharatmolView Answer on Stackoverflow
Solution 6 - JavascriptprograhammerView Answer on Stackoverflow
Solution 7 - JavascriptChris RoseteView Answer on Stackoverflow
Solution 8 - JavascriptMatiasVView Answer on Stackoverflow
Solution 9 - JavascriptAndreas LyngstadView Answer on Stackoverflow
Solution 10 - JavascriptMHoweyView Answer on Stackoverflow