Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you reset a Chosen multiple select field using jQuery?

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?

like image 457
jay Avatar asked Sep 22 '12 12:09

jay


1 Answers

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

like image 130
DONKEYSAURUS Avatar answered Sep 27 '22 19:09

DONKEYSAURUS