1

I am trying to get the "value" attribute in an tag (inside a tag.

My current code:

JavaScript:

var TheId = $('.nameofselect option:selected').val();

HTML:

<option value="123">Text value</option>

But this is returning the "Text value" of the option, not the actual value (i.e. 123) - which is what I need.

Thanks in advance

10
  • 1
    hat should work, although you don't need option:selected. Just $('.classofselect').val(). Commented Jul 3, 2014 at 5:52
  • 2
    I can't reproduce the problem: jsfiddle.net/barmar/4ApJy/1 Can you make a fiddle that demonstrates the problem? Commented Jul 3, 2014 at 5:55
  • Do you have class="nameofselect" in the <select> element? Commented Jul 3, 2014 at 5:56
  • @Barmar If it don't have that class then how it would have returned Text value... Quite confusing.. Commented Jul 3, 2014 at 5:57
  • I don't know. He must have done something wrong, and he didn't copy it to the question. Commented Jul 3, 2014 at 5:58

2 Answers 2

1

Use .attr() selector

Try this

$('.nameofselect option:selected').attr('value'); // Return 123

$('.nameofselect').val() // Return 123

Example HTML

<select class="nameofselect">
    <option value="">select</option>
    <option value="123">Text value</option>
</select>

Script

$('.nameofselect').on('change',function(){
   alert( $(this).find('option:selected').val())
   alert( $(this).find('option:selected').attr('value'))
});

DEMO

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

4 Comments

When I try the $('.nameofselect').val(), it says undefined. When I change it to $('#idofselect').val() it displays the text value, not the ID that I want
Not my downvote, but $(this).find('option:selected').val() is just a crazily inefficient way to do this.value.
RobG OP said in that way it shows undefined thats why we answered like that we too know this.value will reurn
The point is that all that jQuery does nothing useful except burn CPU cycles. Direct access to the DOM property removes jQuery as a factor (and is hugely faster and less to type).
0

You can use .val() to get selected value of select tag :

var TheId = $('.nameofselect').val();

And if you want to read value attribute of option then use below :

$('.nameofselect option').each(function(){
  alert($(this).val());
});

2 Comments

The jQuery val method does not necessarily return the value of the value attribute. It returns the attribute if present, or the text content of the option if there is no value attribute. If you want the actual value of an option's value attribute, use option.getAttribute('value'), or the jQuery attr method (in versions after 1.6).
@BhushanKawadkar You don't need to use .each() jQuery will iterate the result set without it.

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.