1
<select id="sel"> <option value="1" test="aaa">dsfsdf</option> <option value="2" test="bbb">ssssss</option> <option value="3" test="ccc">dggggg</option> </select> <span id="check">check</span> $("#check").click(function(){ console.log($("#sel option").attr("selected", true).attr('test')); }) 

LIVE: http://jsfiddle.net/rhqbG/

Now this show me always "aaa". How can i make it?

3 Answers 3

4

You could rewrite it to

console.log($("#sel option:selected").attr('test')); 

http://jsfiddle.net/gYpYV/

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

Comments

1
document.getElementById('sel').value; 

Should do the trick.

EDIT

var sel = document.getElementById('sel'); var opt = sel.options[sel.selectedIndex]; // now get attributes of opt, such as: console.log(opt.getAttribute("test")); 

1 Comment

i want get attribute test, not value
1

Just to note, it's poor practice to include non-conforming custom attributes on elements. If you're using HTML 5 you can use "data-" custom attributes, but otherwise it would be preferable to maintain a JavaScript hash of values associated with the element.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.