Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the value of select tag jQuery [duplicate]

Tags:

jquery

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?

like image 645
Mausumi Avatar asked Jul 17 '13 06:07

Mausumi


3 Answers

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();
like image 171
kuldeep raj Avatar answered Sep 28 '22 06:09

kuldeep raj


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!

like image 27
Lars Ebert Avatar answered Sep 28 '22 07:09

Lars Ebert


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()
like image 36
Nithesh Narayanan Avatar answered Sep 28 '22 07:09

Nithesh Narayanan