I have a code like
<select id="sel1">
<option label="l1" value="1">Cool</option>
<option label="l2" value="2">Hot</option>
</select>
Now I want to get the value should be either Cool or Hot while changing the value of selected item in jQuery.
I am trying by querySource = $("#querySource").val();
but this is showing as 1
or 2
.
How to do that?
Please use this one :
If you want to get text value : like Cool and Hot then ues this one
$('#sel1 option:selected').text();
If you want to get value : like 1,2 then
$('#sel1 option:selected').val();
Through .val()
you will receive the value of the value attribute. You can fetch the text through .text()
.
Use this method on your selected option! See this answer!
Use option:selected
querySource = $("#querySource option:selected").text();
Also you may use the followings
$("#querySource option").is("selected").text()
$("#querySource").children("option").is("selected").text()
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