Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear a multiple select dropdown using jquery [duplicate]

Tags:

jquery

Possible Duplicate:
Quick way to clear all selections on a multiselect enabled <select> with jQuery?

Dear All.

I have a problem. once i have a multiselect box and i want to clear all the contents of that when i clickon the reset button.

could you please provide me jquery for that?

Thanks.

Satya

like image 848
Satya Avatar asked May 19 '11 17:05

Satya


People also ask

How can we prevent duplicate values in multiple dropdowns in jQuery?

The below codes works fine for two dropdowns, but not for more than 2. var $select = $("select[id$='go']"); $select. change(function() { $select . not(this) .

How do I remove duplicate values from a drop down list in HTML?

You also can remove the duplicates from the table list firstly, then create the drop down list. Select the column range you want to use in the table, the press Ctrl + C to copy it, and place it to another position by pressing Ctrl + V. Then keep selecting the list, click Data > Remove Duplicates.


2 Answers

I assume you mean clear selection:

//for jQuery >= 1.6
$('select option').removeProp('selected');
//for jQuery < 1.6
$('select option').removeAttr('selected');
like image 115
Naftali Avatar answered Oct 06 '22 14:10

Naftali


To unselect all items if using jQuery 1.6.1 or later:

$('#mySelect').children().removeProp('selected');

or for backwards compatibilty:

$('#mySelect').children().removeAttr('selected');

For simplicity's sake I'm assuming that your HTML is well formed and that your select only has option children.

Also, if the list is large and performance is an issue, filter the list so that you only change the ones that are actually selected - it's arguable whether it's simpler to simply deselect every option or only change the ones that need deselecting:

$('#mySelect').children(':selected').removeProp('selected');

[NB: using :selected in the second function call is (according to the jQuery docs) more efficient than making a single compound selector]

like image 8
Alnitak Avatar answered Oct 06 '22 15:10

Alnitak