gespinha
gespinha

Reputation: 8497

How to get val() of option in jQuery object array

I have the following select tag:

<select id="myselect">
    <option value="1">Mr</option>
    <option value="2">Mrs</option>
    <option value="3">Ms</option>
    <option value="4">Dr</option>
    <option value="5">Prof</option>
</select>

How can I console.log the value of option tag number 2? I am trying this, but it isn't working.

$(document).ready(function () {
    console.log($("#myselect option")[2].val());
});

http://jsfiddle.net/u44pu1os/3/

Upvotes: 0

Views: 30

Answers (2)

depperm
depperm

Reputation: 10756

This should do it

console.log($('#myselect option[value="2"]').html());

Upvotes: 0

Abhishekh Gupta
Abhishekh Gupta

Reputation: 6236

You can try this:

$(document).ready(function () {
    console.log($("#myselect option:eq(1)").attr("value"));
});

Upvotes: 4

Related Questions