Rawand Hawiz
Rawand Hawiz

Reputation: 3

Getting second value in jQuery dropdown

How would I get the second value in a dropdown list using javascript or jquery?

<select id='dropdown'>
<option selected>Any</option>
<option>2</option>
<option>3</option>
</select>

e.g. i would like to get the value of number 2 in the dropdown

Upvotes: 0

Views: 7841

Answers (5)

Milson
Milson

Reputation: 1575

Why not try this so easy to understand then :eq(n)

http://jsfiddle.net/q9qCR/2/

$('#dropdown option:nth-child(2)').val()

Upvotes: 1

palaѕн
palaѕн

Reputation: 73896

To get the second value in a dropdown list using jquery, you can do this using .eq():

var text = $("#dropdown option").eq(1).text();

In order, to get the n th number value in the dropdown list, you can do this:

var text = $("#dropdown option").eq(n - 1).text();

Upvotes: 2

Tamil Selvan C
Tamil Selvan C

Reputation: 20199

Try

$(document).ready(function() {
  alert($("#dropdown option").eq(1).text());
});

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074178

Neither of those options has a value. (Actually, see note below.) You can get the text like this:

var text = $("#dropdown")[0].options[1].text;

Or without jQuery:

var text = document.getElementById("dropdown").options[1].text;

Live Example | Live Source

Or you can use .value instead of .text, as the option elements will default their value property if you don't give them a value attribute. (You can't select them using a value= selector, but the property is defaulted.)

Upvotes: 4

Codegiant
Codegiant

Reputation: 2150

Try this

JS CODE

$(function(){
    alert($('#dropdown option').eq(1).val());
});

LIVE DEMO

Upvotes: 1

Related Questions