Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the value of a dropdown in jQuery

Tags:

jquery

I have a drop down that has an 'ID, Name' Pair.

Example

Jon Miller
Jim Smith
Jen Morsin

Jon MIller has ID of 101
Jim Smith has ID of 102
Jen Morsin has ID of 103

When I do the followng:

var arNames = $('#Crd').val()  

and I select Jon Miller, I get 101. I'd like to get Jon Miller though.

like image 964
Nate Pet Avatar asked Jan 23 '12 20:01

Nate Pet


People also ask

How do I get the selected value of dropdown?

The value of the selected element can be found by using the value property on the selected element that defines the list. This property returns a string representing the value attribute of the <option> element in the list. If no option is selected then nothing will be returned.

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 do I select a specific Dropdownlist using 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 do I get the selected value of multiple dropdowns in jQuery?

With jQuery, you can use the . val() method to get an array of the selected values on a multi-select dropdown list.


2 Answers

$('#Crd').val() will give you the selected value of the drop down element. Use this to get the selected options text.

$('#Crd option:selected').text(); 
like image 107
ShankarSangoli Avatar answered Sep 28 '22 21:09

ShankarSangoli


The best way is to use:

$("#yourid option:selected").text(); 

Depending on the requirement, you could also use this way:

var v = $("#yourid").val(); $("#yourid option[value="+v+"]").text() 
like image 35
Sandeep Pal Avatar answered Sep 28 '22 20:09

Sandeep Pal