Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get value of HTML select option using its index with jQuery [closed]

HTML for select and options:

<select class="dropdown">
    <option value="one">One</option>
    <option value="two">Two</option>
    <option value="three">Three</option>
</select>

I want the value of the option at index 1, something along the lines of

$('.dropdown[1]').value
// should return 'two'
like image 958
frostbite Avatar asked Aug 29 '13 20:08

frostbite


People also ask

How do I get the text value of a selected option jQuery?

$var = jQuery("#dropdownid option:selected"). val(); alert ($var); Or to get the text of the option, use text() : $var = jQuery("#dropdownid option:selected").

How can check select option value is selected or not in jQuery?

$('#mySelectBox option'). each(function() { if ($(this). isChecked()) alert('this option is selected'); else alert('this is not'); });

How do you select a particular option in a select element in jQuery?

Syntax of jQuery Select Option$(“selector option: selected”); The jQuery select option is used to display selected content in the option tag. text syntax is below: var variableValue = $(“selector option: selected”).

How can I get the selected value of a drop down list with jQuery?

Use the jQuery: selected selector in combination with val () method to find the selected option value in a drop-down list.


3 Answers

$('.dropdown option').eq(1).val()

eq() will start with 0 as it is an index

Get the currently selected value with

$('.dropdown option:selected').val()

use text() instead of val() to retrieve the text content

like image 116
Horen Avatar answered Oct 06 '22 04:10

Horen


To get the text:

var list = document.getElementById("dropdown");
var value = list.options[1].text;
like image 42
Xynariz Avatar answered Oct 06 '22 02:10

Xynariz


You can use this to get the value:

$('select.dropdown option').eq(1).val();

An this to get the text:

$('select.dropdown option').eq(1).text();

Demo here

like image 36
Sergio Avatar answered Oct 06 '22 02:10

Sergio