0

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/

2
  • Try this: `console.log($("#myselect option:selected").val()); Commented Jun 5, 2015 at 17:55
  • @holpducki The question asks specifically for the value of the second option, not the selected option (whatever that might be). Commented Jun 5, 2015 at 17:57

2 Answers 2

4

You can try this:

$(document).ready(function () {
    console.log($("#myselect option:eq(1)").attr("value"));
});
Sign up to request clarification or add additional context in comments.

Comments

0

This should do it

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

Comments

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.