Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the selected value in a dropdown using jQuery.

My HTML is below. I need to get the selected value (Scheduled) using the <select> tag. How can this be done using jQuery?

<select id="availability" style="display: none;">   <option value="Available">Available</option>   <option selected="selected" value="Scheduled">Scheduled</option>   <option value="Unavailable">Unavailable</option> </select> 

I did a jQuery("#availability") to get the select tag, but I do not know how to get the selected options' value.

like image 986
bragboy Avatar asked Aug 18 '10 14:08

bragboy


People also ask

How do I get the selected value of dropdown?

The value of the selected element can be found by using the value property on the selected element that defines the list. This property returns a string representing the value attribute of the <option> element in the list.

How show selected value from database in dropdown using jQuery?

Use $('select[id="salesrep"]'). val() to retrieve the selected value. Use $('select[id="salesrep"]'). val("john smith") to select a value from dropdown.

How do I get the selected value and current selected text of a dropdown box using jQuery?

$var = jQuery("#dropdownid option:selected"). val(); alert ($var); Or to get the text of the option, use text() : $var = jQuery("#dropdownid option:selected").

How do I get the selected value of multiple dropdowns in jQuery?

With jQuery, you can use the . val() method to get an array of the selected values on a multi-select dropdown list.


2 Answers

Try:

jQuery("#availability option:selected").val(); 

Or to get the text of the option, use text():

jQuery("#availability option:selected").text(); 

More Info:

  • http://api.jquery.com/val/
  • http://api.jquery.com/text/
like image 98
Sarfraz Avatar answered Sep 24 '22 00:09

Sarfraz


The above solutions didn't work for me. Here is what I finally came up with:

$( "#ddl" ).find( "option:selected" ).text();           // Text $( "#ddl" ).find( "option:selected" ).prop("value");    // Value 
like image 44
Mahdi Avatar answered Sep 23 '22 00:09

Mahdi