Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery: determining which option of drop down is selected

Tags:

jquery

How do you determine which option is selected in a drop-down box using jquery?

Thanks.

like image 946
frenchie Avatar asked Feb 24 '23 16:02

frenchie


1 Answers

$('#mySelect').val();

Will give the value associated with the selected option (the value attribute if one exists, or the text otherwise).


$('#mySelect')[0].selectedIndex;

Will give the index of the selected option.


$('#mySelect option:selected');

Will give the selected option element, from which you can grab:

$('#mySelect option:selected').text(); // the text
$('#mySelect option:selected').val(); // the value
like image 185
David Tang Avatar answered Mar 11 '23 18:03

David Tang