Julian Hinderer
Julian Hinderer

Reputation: 169

jQuery and preselected dropdown

I have some trouble with a HTML dropdown and jQuery. One option is preselected

<select id="category">
 <option value="1" selected="selected">Value 1</option>
 <option value="2">Value 2</option>
</select>

I try to get the selected value with this line

$("#category").val();

But I get always the preselected option "1". Also variants like

$("select option:selected").val();

don't work.

Any suggestions?

Upvotes: 0

Views: 260

Answers (2)

Jo&#227;o Silva
Jo&#227;o Silva

Reputation: 91349

$("#category").val() gives you the selected option value.

Upvotes: 1

thecodeparadox
thecodeparadox

Reputation: 87073

I think you need this:

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

DEMO

Because. $('#category').val() will output you the value attribute of selected option.In order to get the text of that selected option you need above.

Upvotes: 0

Related Questions