Deselecting all elements from a multi-select dropdown in jQuery
Asked Answered
A

10

39

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?

Agape answered 24/11, 2011 at 9:16 Comment(0)
S
56
$("#edit-rec option:selected").removeAttr("selected");
Swung answered 24/11, 2011 at 9:19 Comment(1)
Better to use $("#edit-rec option:selected").prop("selected", false);Purposeful
L
15

Try -

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

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

Lancastrian answered 24/11, 2011 at 9:19 Comment(4)
Your code looks very good. But could you explain the syntax $("#edit-rec > option")Agape
$("#edit-rec > option") should select any option elements that are direct child elements the #edit-rec element. See - api.jquery.com/child-selectorLancastrian
I would suggest removing the > part. It doesn't make sense in this case, and would cause unexpected behavior using <optgroup> as a parent of <option>.Evans
You need to use prop instead of attr these days, but yeah.Billiton
P
10

The easiest way that I found to unselect all options in a multi-select dropdown was using .val([]).

$("#select").val([]);
Punchboard answered 5/4, 2017 at 20:17 Comment(1)
in my case $("select").val([]); worked perfectly, thanks!Rew
A
7

On click of radio button you can use this

$("#edit-rec  option").each(function(){

    this.selected=false;

});
Alkahest answered 24/11, 2011 at 9:29 Comment(0)
H
7

It will remove all checked options from multiselect dropdownlist :

$('#ddlTradeShow').multiselect("clearSelection");

Holeandcorner answered 6/5, 2016 at 10:12 Comment(2)
Please add a bit of explanation to your answer, to make it more readableOrvilleorwell
didn't go through the documentation yet, but it worked :)Snowplow
S
3
$("#edit-rec option:selected").removeAttr("selected");
Snoopy answered 24/11, 2011 at 9:18 Comment(0)
A
3

Can do something like this JS Fiddle for setting up the click on the radio button http://jsfiddle.net/x5ck3/

$('#rdClear').click(

function() {
    $("#edit-rec option:selected").removeAttr("selected");
});
Artiste answered 24/11, 2011 at 9:24 Comment(0)
L
3
$("#butt").click(function () {
    $("#edit-rec > option").removeProp("selected");
});

that's correct with new jQuery version

Lectern answered 24/11, 2011 at 9:28 Comment(1)
very bad idea, from api.jquery.com/removeProp This will remove the property completely and, once removed, cannot be added again to element. Use .prop() to set these properties to false instead.Dowson
D
1

There is now another possibility directly provided by the API :

$('#edit-rec').multiSelect('deselect_all');

Works fine, here you can find more options about it : http://loudev.com/

Decameter answered 19/10, 2016 at 15:15 Comment(0)
B
0

You can use the vanilla HTMLSelectElement DOM interface, and also there's no need to iterate over all of the <option> elements. Either selectedIndex or value can be used:

$('#edit-rec')[0].selectedIndex = -1;

Or:

$('#edit-rec')[0].value = '';

Those will both work regardless of the multiple attribute. See this fiddle for examples of various methods.


PS: For multiple elements you can use jQuery's .each (but be careful to return non-false from the callback), or the DOM collection's .forEach (return values don't matter):

$('.all-the-selects').each((_,s) => s.selectedIndex = -1);

Or:

$('.all-the-selects').get().forEach(s => s.selectedIndex = -1);
Billiton answered 30/5, 2022 at 15:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.