6

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?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Mausumi
  • 431
  • 2
  • 6
  • 20

5 Answers5

8

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();
Kuldeep Raj
  • 791
  • 1
  • 7
  • 29
2

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!

Community
  • 1
  • 1
Lars Ebert
  • 3,487
  • 2
  • 24
  • 46
1

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()
Nithesh Narayanan
  • 11,481
  • 34
  • 98
  • 138
1

Try this

$("#sel1 option:selected").text()
mkHun
  • 5,891
  • 8
  • 38
  • 85
arun
  • 85
  • 2
  • 12
0

you can use this

$("#querySource option:selected").text();
Ghost Answer
  • 1,458
  • 1
  • 17
  • 41