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?

2
  • 2
    alert($("#sel1 option:selected").text()); Commented Jul 17, 2013 at 6:19
  • Please use this code for getting the value $('#sel1 option:selected').text(); Commented Jul 17, 2013 at 6:29

5 Answers 5

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();
Sign up to request clarification or add additional context in comments.

Comments

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!

1 Comment

but if I do it by using .text() then it is showing the whole <select><option..>...</option></select> which should not be expected
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()

Comments

1

Try this

$("#sel1 option:selected").text()

Comments

0

you can use this

$("#querySource option:selected").text();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.