1

How can I get the text within the selected dropdown menu option using jQuery?

I have tried:

var title = $("#selectattribute option:selected").text();

But I don;t think it works..

2
  • @Robin - .val() returns the value, which is not the same as the text inside an option ==> <option value="this">A choice</option> Commented Aug 21, 2011 at 23:58
  • Ah I see. Sorry for misunderstanding. Commented Aug 22, 2011 at 0:37

2 Answers 2

1

What you did should work:

$("select option:selected").text()

Working example

Since it's not working for you, the error must lie somewhere else. Maybe #selectattribute is incorrect.

To clarify some of the other answers, the value of an option is different from the text inside it.

For example:

<select>
    <option value="red" selected="selected">Ferrari</option>
</select>

// For the above HTML
$("select option:selected").text() === 'Ferrari'
$("select option:selected").val()  === 'red'

Also, if no selected attribute is set in the HTML, the first option will be selected:

<select>
    <option value="black">Porsche</option>
    <option value="red"  >Ferrari</option>
</select>

// For the above HTML
$("select option:selected").text() === 'Porsche'
Sign up to request clarification or add additional context in comments.

Comments

0

You can get the value of the select box by simply using:

var title = $("#selectattribute").val();

To get the text of the option, instead of the value attribute:

var title = $("#selectattribute :selected").text();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.