Reputation: 3
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
Reputation: 1575
Why not try this so easy to understand then :eq(n)
$('#dropdown option:nth-child(2)').val()
Upvotes: 1
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
Reputation: 20199
Try
$(document).ready(function() {
alert($("#dropdown option").eq(1).text());
});
Upvotes: 0
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;
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