Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deselecting all elements from a multi-select dropdown in jQuery

I have a multi-select drop down as following, where I have selected the options "Test 2" and "Test 3".

<select id="edit-rec" class="form-select" multiple="multiple" name="rec[]"> <option value="6012">Test 1</option> <option value="8436">Test 2</option> <option value="4689">Test 3</option> <option value="6784">Test 4</option> </select> 

I have a button called "Deselect All". When this button is clicked, all selected items should be deselected. In this case, the items I previously selected, "Test 2" and "Test 3", should now become deselected.

How can I accomplish this using jQuery?

like image 487
Fero Avatar asked Nov 24 '11 09:11

Fero


People also ask

How do I get selected items from select multiple in jQuery?

With jQuery, you can use the . val() method to get an array of the selected values on a multi-select dropdown list.

How remove all options from dropdown in jQuery?

To remove all options from a select list using jQuery, the simplest way is to use the empty() method.

How do I deselect multiple selections in HTML?

You can deselect options in a multi-value select list by ctrl-clicking it.


2 Answers

$("#edit-rec option:selected").removeAttr("selected"); 
like image 66
Chuck Norris Avatar answered Oct 14 '22 06:10

Chuck Norris


Try -

$("#edit-rec > option").attr("selected",false); 

Demo - http://jsfiddle.net/LhSBu/

like image 45
ipr101 Avatar answered Oct 14 '22 05:10

ipr101