Best way to unselect a <select> in jQuery?
Use removeAttr...
$("option:selected").removeAttr("selected");
Or Prop
$("option:selected").prop("selected", false)
There are lots of answers here but unfortunately all of them are quite old and therefore rely on attr
/removeAttr
which is really not the way to go.
@coffeeyesplease correctly mentions that a good, cross-browser solution is to use
$("select").val([]);
Another good cross-browser solution is
// Note the use of .prop instead of .attr
$("select option").prop("selected", false);
You can see it run a self-test here. Tested on IE 7/8/9, FF 11, Chrome 19.
Simplest Method
$('select').val('')
I simply used this on the select itself and it did the trick.
I'm on jQuery 1.7.1.
It's a been a while since asked, and I haven't tested this on older browsers but it seems to me a much simpler answer is
$("#selectID").val([]);
.val() works for select as well http://api.jquery.com/val/
Answers so far only work for multiple selects in IE6/7; for the more common non-multi select, you need to use:
$("#selectID").attr('selectedIndex', '-1');
This is explained in the post linked by flyfishr64. If you look at it, you will see how there are 2 cases - multi / non-multi. There is nothing stopping you chaning both for a complete solution:
$("#selectID").attr('selectedIndex', '-1').find("option:selected").removeAttr("selected");