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.
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.
Use $('select[id="salesrep"]'). val() to retrieve the selected value. Use $('select[id="salesrep"]'). val("john smith") to select a value from dropdown.
$var = jQuery("#dropdownid option:selected"). val(); alert ($var); Or to get the text of the option, use text() : $var = jQuery("#dropdownid option:selected").
With jQuery, you can use the . val() method to get an array of the selected values on a multi-select dropdown list.
Try:
jQuery("#availability option:selected").val();
Or to get the text of the option, use text()
:
jQuery("#availability option:selected").text();
More Info:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With