Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting selected text of a select listbox using JQuery when I already have the select object in hand

Here is an easy one, a true newbie question:

I know that it's possible to get the text of the selection option like this

$('#selectBoxId option:selected').val(); 

But what if I already have the select object in my hand?

e.g.

var select = $('#selectBoxId');

What comes next? this is the right way?

select.find('option:selected').val();
like image 275
Eran Medan Avatar asked Sep 01 '11 20:09

Eran Medan


People also ask

How do I get the selected value and current selected text of a dropdown box using 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'); });


1 Answers

Just select.val(); is enough. It will give you the selected value.

Working demo

If you want the selected option text then your need to do

select.find('option:selected').text();

Or

select.find('option:selected').html();
like image 182
ShankarSangoli Avatar answered Sep 28 '22 18:09

ShankarSangoli