10

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.

2
  • I realize that you're new here, so I'll introduce you to you could accept the answer that you feel best answered the question and earn an additional 2 rep to go along with the 6 you have now ;) ~ check the arrow under the votes on the left side of the page. Commented Feb 5, 2011 at 1:57
  • Thanks, I attempted to do that, was told to wait 41 seconds, and then...nothing happened? EDIT: Apologies, it apparently worked, though it didn't light up right away. Commented Feb 5, 2011 at 2:04

3 Answers 3

13

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

Sign up to request clarification or add additional context in comments.

1 Comment

+1 Thanks for this. I was banging my head against the wall, and this made me relize I had typo'd vlaue instead of value.
2

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')

1 Comment

Yes, that was the problem. Thanks!
2

try this :

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

note : didnt tested

1 Comment

You should test in the future, it works wonders, but +1 for the use of attr('val') cos it would work too ;)

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.