Reputation: 16687
I am think this should be fairly easy, but struggling a bit ... I have a SELECT
input element that allows the user to select multiple items. I would like to provide a mechanism for them to clear all selected items, probably will just be a SPAN
tag or something similar.
Anyway ... using jQuery, how I would I clear the selection on the SELECT
input element so no items are selected.
Upvotes: 35
Views: 127636
Reputation: 4881
Try: // Just put # before select to fix this. Works perfect. $("#select option:selected").each(function () { $(this).remove(); //or whatever else });
Upvotes: 6
Reputation: 162
This solution worked for me...
$('#my-Select').val('').trigger('change');
This works on almost any input type.
Upvotes: 6
Reputation: 595
This worked to clear all selected options for me..
$("#selectListName").prop('selectedIndex', -1)
The select list looked like
<select multiple='multiple' id='selectListName'>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
Upvotes: 14
Reputation: 1810
Better than this, you can use:
$("#selectID").empty();
That works on almost anything in the DOM.
Upvotes: -3
Reputation: 2588
Only this worked for me:-
$("#selectid").find('option').attr("selected",false) ;
Upvotes: 5
Reputation: 630637
In the case of a <select multiple>
the .val()
function takes/returns an array, so you can simply pass in an empty array to clear the selection, like this:
$("#selectID").val([]);
Upvotes: 91
Reputation: 1518
Maybe
$('#mySelect option').each(function(i, e)
{
e.selected = false
});
Upvotes: 0
Reputation: 10342
Try this
<select id='select1' ></select>
<input type= 'button' value = 'Clear Selection' id = 'remove' />
$(document).ready(function() {
$('#remove').click(function() {
return $('#select1 option:selected').remove();
});
});
Upvotes: -1
Reputation: 125674
i try something like
$("#my-Select").find('option').attr("selected","") ;
Upvotes: 3