Quick way to clear all selections on a multiselect enabled <select> with jQuery?
Asked Answered
T

14

50

Do I have to iterate through ALL the and set remove the 'selected' attribute or is there a better way?

Tucci answered 12/2, 2010 at 20:1 Comment(0)
F
78

Simply find all the selected <option> tags within your <select> and remove the selected attribute:

$("#my_select option:selected").removeAttr("selected");

As of jQuery 1.6, you should use .prop instead of removing the attribute:

$("#my_select option:selected").prop("selected", false);
Fulmar answered 12/2, 2010 at 20:4 Comment(3)
for multi select drop down , this will not clear displayed text.Nonperishable
But how do you clear displayed text?Breadstuff
$("#my_select option:selected").prop("selected", false); clears displayed text for me?Depressomotor
L
39

In order to clear all selection, I am using like this and its working fine for me. here is the script:

 $("#ddlMultiselect").multiselect("clearSelection");

 $("#ddlMultiselect").multiselect( 'refresh' );
Lebaron answered 16/6, 2014 at 7:35 Comment(5)
This answer is not relevant to the question.Lauritz
What library is this for? Multiselect isn't part of jQuery or jQueryUI.Esch
This is for bootstrap multiselect may beLaval
worked for me : jQuery(".select-access").multiselect("clearSelection") "s" lowerCase.Gimmick
he's refering to this plugin loudev.com/#usage and btw "clearSelection" don't work now. use "deselect_all" insteadEncarnacion
Q
3

Shortest and quickest way to remove all the selection, is by passing empty string in jQuery .val() function :

$("#my_select").val('')
Quack answered 28/2, 2014 at 10:25 Comment(1)
just a warning that if you have an option with value="", this method will select it so accepted answer is the way for you jsfiddle.net/yemgudc8Rijeka
G
3

This is the best way to clear a multi-select or list box:

 $("#drp_assign_list option[value]").remove();
Gleeful answered 1/10, 2014 at 5:44 Comment(0)
K
3

In JQuery:

  $("#id option:selected").prop("selected", false);
  $("#id").multiselect('refresh');
Krupp answered 14/6, 2016 at 13:24 Comment(0)
M
3

$('.clearAll').on('click', function() {
  $('.dropdown').val([]);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select class="dropdown" multiple>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
</select>

<button class='clearAll'>Clear All Selection</button>
Mezzosoprano answered 6/2, 2020 at 12:10 Comment(1)
Please add some explanation to your answer :)Gnarly
E
2

$("#ddlMultiselect").val('').trigger("change");

for multi-select drop-down, it will clear displayed text.

Endoskeleton answered 18/9, 2020 at 13:32 Comment(0)
A
1

jQuery's :selected selector is probably what you are looking for.

Aegir answered 12/2, 2010 at 20:4 Comment(0)
M
1

In javascript,

You can use the list of all options of multiselect dropdown which will be an Array.Then loop through it to make Selected attributes as false in each Objects.

for(var i=0;i<list.length;i++)
{
   if(list[i].Selected==true)
    {
       list[i].Selected=false;
    }
}
Marchak answered 9/3, 2017 at 14:15 Comment(1)
The question is specifically asking for a way to do it without iterating over the whole list (which could get resource heavy if the list is long)Obedience
B
1

You can pass an empty array to unselect all the selection. Here goes an example. From documentation

val() allows you to pass an array of element values. This is useful when working on a jQuery object containing elements like , , and s inside of a . In this case, the inputs and the options having a value that matches one of the elements of the array will be checked or selected while those having a value that doesn't match one of the elements of the array will be unchecked or unselected, depending on the type.

$('.clearAll').on('click', function() {
  $('.dropdown').val([]);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select class="dropdown" multiple>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
</select>

<button class='clearAll'>Clear All Selection</button>
Beak answered 29/8, 2018 at 21:23 Comment(0)
L
1
$('.selectpicker').selectpicker('deselectAll');

https://developer.snapappointments.com/bootstrap-select/methods/

Lawless answered 13/12, 2018 at 14:19 Comment(1)
This is the one that works if you're using bootstrap-selectFlieger
D
0

I've tried many solutions and came up with this.

Options and selections of the dropdown are cleared using this only

$("#my-multi option:selected").prop("selected", false);
$("#my-multi option").remove();


But the interface is not updated. So you have to do this

$('#my-multi').multiselect('rebuild');


Hence, the final code is

$("#my-multi option:selected").prop("selected", false);
$("#my-multi option").remove();
$('#my-multi').multiselect('rebuild');


Suggestions and improvements are welcome.

Dimarco answered 23/1, 2020 at 7:5 Comment(1)
$('#my-multi').multiselect('rebuild'); this worked for me thanks bro :)Featly
U
0
$("#my_select option:selected").prop("selected", false).change();

Its clear value and display text as well.

Unending answered 3/10, 2023 at 16:8 Comment(0)
W
-1

Assuming you are using Bootstrap multiselect dropdown by David Stutz

$('#selectId').multiselect('deselectAll', false);

But for some reason it does not work inside initialization method

Waac answered 31/8, 2017 at 20:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.