Reputation: 3923
<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?
Upvotes: 1
Views: 366
Reputation: 739
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.
Upvotes: 1
Reputation: 324600
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"));
Upvotes: 1
Reputation: 254886
You could rewrite it to
console.log($("#sel option:selected").attr('test'));
Upvotes: 4