3

I want to get attribute value of selected option element.

jQuery:

$("#select_element").val().attr("val");

HTML:

 <select id="select_element" class="selectpanel">
     <option val="x">element1</option>
     <option val="y">element2</option>
 </select>
5
  • Remove the attr method and use value attribute not val. Commented Nov 6, 2014 at 12:50
  • 1
    I Googled the title of your question and found the answer to your problem, you should do the same. stackoverflow.com/questions/2230704/… Commented Nov 6, 2014 at 12:50
  • possible duplicate of How do you select a particular option in a SELECT element in jQuery? Commented Nov 6, 2014 at 12:53
  • val is not a valid attribute for option elements. The attribute is value. If you want a custom attribute, use a data- prefix. Commented Nov 6, 2014 at 12:55
  • T.J. Crowder thanks for information Commented Nov 6, 2014 at 13:14

4 Answers 4

4

Try this:

$('#select_element option:selected').attr('val');

Or

$('#select_element option:selected').val();

both are valid!

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

1 Comment

Yes its work.thanks var id = $('#ilce option:selected').attr("id");
2

Simple use:

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

If you want the text of it, then:

$('#select_element option:selected').text();

JSFIDDLE

4 Comments

What is this? your option has no attribute id.
I changed <option val="04" id="04">x</option>
In this case, much more better, if you are using data-id and get $('#select_element option:selected').data('id');
But you did not mentioned this, and also do not need this. Check my edited answer, with jsfiddle link.
0

try this for get the value in the options

$("#select_element").val()

And try this for get the text in te option selected

$('#slcFoo option:selected').text()

You can see the example here

Comments

0

Re your edit showing:

<option val="x">element1</option>

val is not a valid attribute for option elements. The correct attribute is value, or if you want a custom attribute, use a data- prefix (data-val, for instance).

So depending on whether you meant value or data-val:

If you meant value:

$("#select_element").val() does give you the value (value attribute) of the selected option element in the select box:

console.log($("#select_element").val());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select id="select_element">
  <option value="1">One</option>
  <option value="2" selected>Two</option>
  <option value="3">Three</option>
</select>

If you meant data-val:

If you wanted to get some other attribute from that same option element, you'd do it like this:

var otherAttributeValue = $("#select_element option:selected").attr("data-val");

E.g.:

var otherAttributeValue = $("#select_element option:selected").attr("data-val");
console.log(otherAttributeValue);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select id="select_element">
  <option data-val="first" value="1">One</option>
  <option data-val="second" value="2" selected>Two</option>
  <option data-val="third" value="3">Three</option>
</select>

1 Comment

@AlexFilatov: Both of the snippets above do work. I briefly had a non-snippet version of the second one which used an argument rather than this (because I misremembered jQuery's filter vs. Array#filter), perhaps that's what you were seeing? If so, hit refresh. But actually, jQuery has the pseudo-selector :selected, so...

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.