Mark Fondy
Mark Fondy

Reputation: 3923

How to get parameter of current selected SELECT?

<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

Answers (3)

Mad Man Moon
Mad Man Moon

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

Niet the Dark Absol
Niet the Dark Absol

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

zerkms
zerkms

Reputation: 254886

You could rewrite it to

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

http://jsfiddle.net/gYpYV/

Upvotes: 4

Related Questions