Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get selected text of a combo box using jQuery, by name of the combo box?

How Can I get selected text value from the combo box using jQuery.

I am having only "name" of combo box.

So, I want the text of selected item, using the name of combo box, not ID.

I am having ,

var selected_fld = ( $(this).attr('name') );

How can I proceed further ?

like image 555
Tech Care Avatar asked Jan 13 '12 11:01

Tech Care


Video Answer


2 Answers

$('select[name=nameOfTheBox]').val();

or

$('select[name=nameOfTheBox] option:selected').val();

will give you the value of the selected option

$('select[name=nameOfTheBox] option:selected').text();

will give you its text

like image 165
Constantin Groß Avatar answered Nov 03 '22 15:11

Constantin Groß


This can be done simply with the following to get the actual text value...

var value = $("[name='MyName'] option:selected").text();

or this to get the option 'value' attribute...

var value = $("[name='MyName']").val();

With the following html, the first will give you 'MyText', the second will give you 'MyValue'

<select name="MyName">
   <option value="MyValue" selected="selected">MyText</option>
</select>

Here is a working example

like image 36
musefan Avatar answered Nov 03 '22 15:11

musefan