6

I have a form with a multiple select field, created using Formtastic and Chosen.

This form has a multiple select field, here is the rails code for it:

 = semantic_form_for 'post', :url => action_name_post_path(@post), :html => {:method => :put}, :remote => true do |f|
    = f.input :blogs, :label => _("Blog"), :as => :select, :multiple => :true, :input_html => {:class => "chzn-select"}, :collection => Blog.all

I would like to reset the input field using jQuery (the form is submitted and reset remotely), and I can't seem to figure out how to remove the selected elements//clear the input field. The issue is that chosen changes the input field so it isn't a simple text area.

Can anyone point me in the right direction?

j0k
  • 22,600
  • 28
  • 79
  • 90
jay
  • 12,066
  • 16
  • 64
  • 103

11 Answers11

14

Seems like combination of the two answers works for me:

$('.chosen-select option').prop('selected', false).trigger('chosen:updated');

This deselects all options and also resets the chosen drop down.

http://jsfiddle.net/donkeysauras/j9yuL/ <-- working example

DONKEYSAURUS
  • 141
  • 1
  • 3
  • The answer by suke is a better option; this method will be very slow if you have many options in your select menus – Erik Apr 02 '14 at 17:00
  • 1
    yeah.. it is slow, you don't need to call the trigger for each option element. You have to trigger only for the select element, then it is blazing fast. Here is my edit: http://jsfiddle.net/j9yuL/126/ – MilMike Aug 05 '16 at 08:06
13

Try this

$('.chzn-select').val('').trigger('liszt:updated');
Mr. 14
  • 9,228
  • 6
  • 37
  • 54
3

This worked for me:

$('form#form_id').find('select').each(function(i, element) {
    $(element).chosen('destroy');
    $(element).prop("selectedIndex", -1);
    $(element).chosen();
});
Hassan Akram
  • 641
  • 6
  • 19
1

You can reset the multiple select using jQuery as follows

$('.chzn-select option').prop('selected', false);

Where the class name of your select is chzn-select.

An working example here.

The Alpha
  • 143,660
  • 29
  • 287
  • 307
1

from documentation

Updating Chosen Dynamically

If you need to update the options in your select field and want Chosen to pick up the changes, you'll need to trigger the "chosen:updated" event on the field. Chosen will re-build itself based on the updated content.

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

In my case it was

jQuery("#my-select-box").trigger("chosen:updated");

my-select-box is id of selectbox.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105
1

This is the only thing that worked for me. multiselect values are an array so you should do it this way:

$('#elementID').val([]).trigger('chosen:updated');

Amir Tofighi
  • 287
  • 2
  • 4
0

For whoever may have ended up on this question (there are multiple and they need to be combined), Chosen has updated their syntax--use this instead:

$('.chzn-select').val('').trigger('chosen');

where '.chzn-select' can be switched out for 'select', '#yourSelect' or whatever other method of selection you wish to use.

Check out:

Community
  • 1
  • 1
matthewsheets
  • 4,535
  • 2
  • 15
  • 11
0

This works fine to me

$('.reset-btn').click(function(){
    var form = $(this).closest('form').get(0);
    form.reset();
    $('.chosen-select').trigger('chosen:updated');
});

Basically i force the form reset and update chosen after reset. :D

Waldemar Neto
  • 826
  • 6
  • 17
0

It all depends on your chosen settings. It worked for me:

$('.chzn-select-no-search').val('').trigger('liszt:updated');
JarekMaxiM
  • 36
  • 2
0

This solution worked for me

$('#.chosen-select').val('').prop('selected', false).trigger('chosen:updated')
Azam Alvi
  • 6,918
  • 8
  • 62
  • 89
0

Only this could work:

$('#client_filter').html(' '); //this worked

NOT WORKING:

$('#client_filter').val('').trigger('change') ; //not working
$('#client_filter').val('').trigger('chosen:updated') ; //not working
$('#client_filter').val('').trigger('liszt:updated') ; //not working
Deepa MG
  • 188
  • 1
  • 15