85

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" />
double-beep
  • 5,031
  • 17
  • 33
  • 41
P H
  • 963
  • 1
  • 7
  • 8
  • 2
    You should change your accepted answer to Jack O'Neill's answer. The other solutions do not work any more. – James Oct 30 '13 at 12:13
  • 1
    Please change your accepted ans to Jack O'Neill answere. because the latest version trigger now called :updated – Well Wisher Nov 09 '13 at 04:40
  • 1
    @Jame & Well wisher: It's not like we will change all answered questions on this site whenever a plugin updates. The solution was correct at the time. RobertWaddell put a comment on the answer, I believe this is the way to go. – GôTô Jan 02 '14 at 13:06

10 Answers10

194

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");
DDA
  • 991
  • 10
  • 28
sottenad
  • 2,646
  • 2
  • 16
  • 18
55

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");
Thermech
  • 4,371
  • 2
  • 39
  • 60
Jack O'Neill
  • 559
  • 4
  • 2
  • 1
    This should be the correct answer due to the changes. None of the other solutions I found work correctly. Thank you! – James Oct 30 '13 at 12:12
  • 1
    But not work! If the select is dinamically replaced with another select (jQuery load() method) with the same id/name, the search stop working on the new select. Tried to call .chosen() on the new select, to call .chosen('destroy'), to call .trigger("chosen:updated"), and combinations of these. – Marco Marsala Oct 07 '14 at 07:03
14

You could try this:

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

$('select').prop("selectedIndex", -1);   
$('select').chosen();
Joel Kennedy
  • 1,581
  • 5
  • 19
  • 41
Edgar O
  • 335
  • 3
  • 3
11

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');
    });
}
buzdykg
  • 623
  • 6
  • 13
  • 1
    Using form.reset() and then triggering chosen:update is the best solution especially if you want to present the form as it was originally displayed. Setting .val('') does not return the form to the default values. –  Aug 24 '14 at 16:14
  • This is working. Remember to manually reset comboboxes (select) dinamically added to DOM. – Marco Marsala Oct 07 '14 at 06:56
1
$("#Your_id").trigger("chosen:updated");

This worked for me

0

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.

prograhammer
  • 20,132
  • 13
  • 91
  • 118
0

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");
Chris Rosete
  • 1,240
  • 15
  • 13
0

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();
            }
MatiasV
  • 9
  • 3
-1

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'});
Andreas Lyngstad
  • 4,887
  • 2
  • 36
  • 69
-2

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("");
});
MHowey
  • 681
  • 2
  • 6
  • 19
  • $j('.chzn-select').each(function () { $j(this).val("") }); does not clear the tags in the chosen – P H Oct 26 '11 at 02:11
  • @P H if you could give an example of your code for us to help – MHowey Oct 26 '11 at 02:25
  • sottenad just answered this, pls refer to his jsfiddle link above. now my code looks like $j('.chzn-select').each(function () { $j(this).val("").trigger("liszt:updated"); }); – P H Oct 26 '11 at 03:10