Get current value selected in dropdown using jQuery

I have a set of dynamically generated dropdowns on my page. I basically cloned them using jQuery. now I want to commit the value selected in each dropdown list of change events.

I tried something like this that didn't work.

$('._someDropDown').live('change', function(e) { //debugger; var v = $(this); alert($(this + ':selected').val()); alert($(this).val()); }); 

How should I do it?

+53
jquery
02 Feb '11 at 11:51
source share
10 answers

This is what you need:)

 $('._someDropDown').live('change', function(e) { console.log(e.target.options[e.target.selectedIndex].text); }); 

For new use of jQuery on

 $(document).on('change', '._someDropDown', function(e) { console.log(this.options[e.target.selectedIndex].text); }); 
+61
Feb 02 '11 at 12:00
source share

To get the text of the selected option

 $("#your_select :selected").text(); 

To get the value of the selected option

 $("#your_select").val(); 
+96
Jan 15 '12 at 3:25
source share
 $("#citiesList").change(function() { alert($("#citiesList option:selected").text()); alert($("#citiesList option:selected").val()); }); 

citiesList is the identifier of the select tag

+15
Feb 04 '14 at 7:06
source share

Check it out →

To get text

 $("#selme").change(function(){ $(this[this.selectedIndex]).text(); }); 

To get the value

 $("#selme").change(function(){ $(this[this.selectedIndex]).val(); }); 
+5
Feb 02 '11 at 12:48
source share

You can try:

 $("._someDropDown").val(); 
+4
Feb 02 '11 at 12:00
source share

To get the value of a select element, just use val ().

 $('._someDropDown').live('change', function(e) { alert($(this).val()); }); 

If you want the text of the selected parameter using this:

 $('._someDropDown').live('change', function(e) { alert($('[value=' + $(this).val() + ']', this).text()); }); 
+4
02 Feb '11 at 12:07
source share

It is actually more efficient and has better readability, in my opinion, if you want to access your choice using this or another variable

 $('#select').find('option:selected') 

In fact, if I remember correctly, phpStorm will try to automatically fix another method.

+3
Apr 21 '16 at 19:52
source share

If you need an index of the currently selected value.

 $selIndex = $("select#myselectid").prop('selectedIndex')); 
+3
01 Oct '17 at 18:09 on
source share

try it...

 $("#yourdropdownid option:selected").val(); 
+2
Feb 02 '11 at 12:02
source share

The options described above will not work because they are not part of the CSS specification (this is a jQuery extension). After spending 2-3 days searching for information, I found that the only way to select the text of the selected option from the drop-down list is:

 { $("select", id:"Some_ID").find("option[selected='selected']")} 

Refer to the additional notes below: Because: selected is a jQuery extension and not part of the CSS specification, queries using: selected cannot take advantage of the performance enhancement provided by the DOM querySelectorAll () built-in method. For best performance when using: selected to select elements, first select elements using a clean CSS selector, then use .filter(":selected") . (copied from: http://api.jquery.com/selected-selector/ )

+1
Nov 08 '13 at
source share



All Articles