Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery how to remove :selected attribute and add new one properly

Tags:

jquery

How do I remove :selected attribute and add new one?

like image 223
Somebody Avatar asked May 16 '10 12:05

Somebody


1 Answers

You want something like

$('#mySelect option:selected').removeAttr('selected');

$('#mySelect option:nth-child(5)').attr('selected','selected');//select the one you want

btw, are you talking about a select list single (e.g. dropdown)?... or a select multiple? - you'll need to adjust accordingly if this is a multi-select.

I updated the nth child selector... when setting the selected attribute as square bracket notation [4] doesn't work in the selector.

like image 164
scunliffe Avatar answered Oct 06 '22 12:10

scunliffe