Get dropdown value in jQuery

I have a drop down that has a pair of ID, Name.

Example

John Miller
Jim smith
Jen morseen

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

When I do followng:

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

and I choose John Miller, I get 101. I would like to get John Miller.

+87
jquery
Jan 23 2018-12-12T00:
source share
11 answers

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

 $('#Crd option:selected').text(); 
+164
Jan 23 2018-12-12T00:
source share

Best used:

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

Depending on the requirement, you can also use this method:

 var v = $("#yourid").val(); $("#yourid option[value="+v+"]").text() 
+11
May 20 '13 at 2:25
source share

If you use <select> , .val() gets the "value" of the selected <option> . If it has no value , it can roll back to id . Put the value you want to return in the value attribute of each <option>

Change: see Comments for a clarification that value is actually (not necessarily equal to the value attribute).

+5
Jan 23 2018-12-12T00:
source share

It worked for me!

 $('#selected-option option:selected').val() 

Hope this helps someone!

+4
Apr 19 '17 at 6:02
source share
 var sal = $('.selectSal option:selected').eq(0).val(); 

selectSal is the class.

+3
Jan 17 '14 at 13:17
source share

Try the following:

 var text = $('#YourDropdownId').find('option:selected').text(); 
+2
Nov 12 '15 at 6:21
source share

Another variant:

 $("#<%=dropDownId.ClientID%>").children("option:selected").val(); 
+1
Feb 21 '17 at 20:19
source share

As stated ... in the select field, the .val() attribute .val() provide you with the value of the selected parameter. If the selected option does not have a value attribute, it will by default be displayed with the parameter value (which is an example for the jQuery .val show documentation .

you want to use the .text() selected option:

$('#Crd option:selected').text()

0
Jan 23 2018-12-21T00:
source share

This gives you the current value of the drop-down list if the drop-down list has the identifier "someId".

 $("#someId").val(); 
0
Nov 25 '13 at 11:31
source share

Pass the identifier and hold it in a variable and pass the variable wherever you want.

var temp = $ ('select [name = ID Name]'). val ();

0
Jun 22 '19 at 7:18
source share

 $('.leave_response').on('change', function() { var responseId = $(this).val(); console.log(responseId); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <td> <select class="leave_response" name="leave"> <option>1</option> <option>2</option> <option>3</option> <option>4</option> </select> </td> 
-one
Jan 04 '18 at 9:26
source share



All Articles