Press "Enter" to skip to content

Get and Set the select box options using jQuery

Getting and setting the value and the text of a selectbox using jQuery is very simple. Get selectbox value, get selectbox text, set selectbox value, set selectbox text, get selectedIndex, set selectedIndex ,etc. using jQuery are mentioned in this article.

get selected option value using jQuery:

$('#selectBox').val();

get selected option text using jQuery:

$('#selectBox > option:selected').text();

set selected option text using jQuery:

var arg ='option Text';
 $('#selectBox > option').each(function(){
 if($(this).text()==arg) $(this).parent('select').val($(this).val())
 })

set selected option value using jQuery:

var arg ='option value';
 $('#selectBox').val(arg)

get selectedIndex using jQuery:

$('#selectBox > option:selected').prevAll().length

set selectedIndex using jQuery:

var index =3;
 $('#selectBox > option').eq(index).attr('selected','selected')

Note: The Above code selects the html selectbox having the id attribute as ‘selectBox’.