Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current value selected in dropdown using jQuery

Tags:

jquery

I have a set of dynamically generated dropdown boxes on my page. basically I clone them using jQuery. now I want to capture the value selected on each dropdown on change event.

I tried something like this which did not work.

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

How do I get it done?

like image 687
Amit Avatar asked Feb 02 '11 11:02

Amit


People also ask

How do you find the current value of a dropdown?

To get the value of a select or dropdown in HTML using pure JavaScript, first we get the select tag, in this case by id, and then we get the selected value through the selectedIndex property. The value "en" will be printed on the console (Ctrl + Shift + J to open the console).

How do I get the selected value and current selected text of a dropdown box using jQuery?

We can select text or we can also find the position of a text in a drop down list using option:selected attribute or by using val() method in jQuery. By using val() method : The val() method is an inbuilt method in jQuery which is used to return or set the value of attributes for the selected elements.

How show selected value from database in dropdown using jQuery?

Use $('select[id="salesrep"]'). val() to retrieve the selected value. Use $('select[id="salesrep"]'). val("john smith") to select a value from dropdown.

How do you check if a dropdown is selected in jQuery?

$('#mySelectBox option'). each(function() { if ($(this). isChecked()) alert('this option is selected'); else alert('this is not'); });


2 Answers

To get the text of the selected option

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

To get the value of the selected option

$("#your_select").val(); 
like image 58
Robin Rizvi Avatar answered Sep 21 '22 18:09

Robin Rizvi


This is what you need :)

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

For new jQuery use on

$(document).on('change', '._someDropDown', function(e) {     console.log(this.options[e.target.selectedIndex].text); }); 
like image 33
Olical Avatar answered Sep 20 '22 18:09

Olical