persephone
persephone

Reputation: 103

Getting the selected option value (NOT text) from dropdown with jQuery

I have a dropdown like so:

<select id="dropdownList">
  <option val="1">Item One</option>
  <option val="2">Item Two</option>
  <option val="3">Item Three</option>
</select>

When I use $('#dropdownList').val(), the value returned is "Item One/Two/Three" rather than the actual option value (1/2/3), which is what I need. I'm not sure if I should be using something other than .val()? I apologize if this has been answered somewhere, but my Google-fu is failing me on this one.

Upvotes: 10

Views: 5960

Answers (3)

Shlomi Komemi
Shlomi Komemi

Reputation: 5545

try this :

$('#dropdownList option:selected').attr('val')

note : didnt tested

Upvotes: 2

jcolebrand
jcolebrand

Reputation: 16035

Try this instead:

<select id="dropdownList">
  <option value="1">Item One</option>
  <option value="2">Item Two</option>
  <option value="3">Item Three</option> <!-- this works -->
  <option val="3">Item Three</option>   <!-- this is what you HAD before -->
</select>

Or if that's not an option, then get the selected index and look for the $(this).attr('val')

Upvotes: 2

The Scrum Meister
The Scrum Meister

Reputation: 30131

Change your html to the valid value= instead of val=

Upvotes: 13

Related Questions