2

I'm trying to get not just the value of the option element, but the value of the label attribute as well. Any ideas on how to do it? I'm currently using this (inline) to get the option's value:

$('select :selected').html(); 

My select would look something like this:

<select> <option value="John Smith" label="JS">John Smith</option </select> 

Any ideas? I've been looking for the last half-hour or so and some solutions have come close, but I can't find anyone who is looking for the value of the label.

Here's one that I thought would answer my question, but I think they were after something slightly different. - How to get label of select option with jQuery?

Thanks in advance!

1
  • The first answer to this question might offer a possibility... I'd just like to be able to do it inline. I guess I don't have to, but that would make it easier. stackoverflow.com/questions/12125651/… Commented Oct 11, 2012 at 21:52

1 Answer 1

2

Try this

$('select option:selected').attr('label'); 

The .html() you are using just gives the Contents inside the option // John Smith

You need to access the label attribute instead..

Instead use the data-* attributes as the label attribute is not valid for option

<select> <option value="John Smith" data-label="JS">John Smith</option> </select> 

And from javascript get the data using:

$('select option:selected').attr('data-label'); 

or alternatively

$('select option:selected').data('label'); $('select option:selected').val(); // Get's the value.. 

CHECK DEMO

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

1 Comment

Ah... I see. I'll give it a try and see how it goes. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.