Jquery: determining which dropdown option is selected

How do you determine which option is selected from the dropdown using jquery?

Thanks.

+4
source share
3 answers
$('#mySelect').val(); 

Gives the value associated with the selected parameter ( value attribute, if one exists, or text otherwise).


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

Gives the index of the selected parameter.


 $('#mySelect option:selected'); 

Gives the selected item an item from which you can grab:

 $('#mySelect option:selected').text(); // the text $('#mySelect option:selected').val(); // the value 
+7
source
 <select id="selectid"> <option value="1">One</option> <option value="2">Two</option> <option value="3">Three</option> </select> 

Assuming the user selects the second option.

Get the value of the selected option

 $("select#selectid").val(); // Returns 2 

Get the text of the selected option

 $("#selectid option:selected").text(); // Returns Two 

jsFiddle Link: http://jsfiddle.net/gpmattoo/LBRCx/

+1
source

Calling the .val() function .val() no parameters in the form element returns the current value of this element. In the case of the select element (drop-down list), this will be the value of the selected option element.

0
source

Source: https://habr.com/ru/post/1345630/


All Articles