Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get $(this) selected option in jQuery?

The following code works:

$("#select-id").change(function(){
  var cur_value = $('#select-id option:selected').text();
  . . .
});

How to refactor the second line to:

var cur_value = $(this).***option-selected***.text();

What do you use for ***option-selected***?

like image 237
B Seven Avatar asked Apr 04 '12 13:04

B Seven


People also ask

How do I get selected option on select Change?

on('change', function () { var selectVal = $("#selectId option:selected"). val(); });

How do I get the select box value?

Answer: Use the jQuery :selected Selector You can use the jQuery :selected selector in combination with the val() method to find the selected option value in a select box or dropdown list.

How can I get multiple selected values of select box in jQuery?

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

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”).


7 Answers

For the selected value: $(this).val()

If you need the selected option element, $("option:selected", this)

like image 63
jorgebg Avatar answered Sep 30 '22 21:09

jorgebg


 $(this).find('option:selected').text();
like image 44
user56reinstatemonica8 Avatar answered Sep 30 '22 22:09

user56reinstatemonica8


Best and shortest way in my opinion for onchange events on the dropdown to get the selected option:

$('option:selected',this);

to get the value attribute:

$('option:selected',this).attr('value');

to get the shown part between the tags:

$('option:selected',this).text();

In your sample:

$("#select-id").change(function(){
  var cur_value = $('option:selected',this).text();
});
like image 33
angelmedia Avatar answered Sep 30 '22 21:09

angelmedia


var cur_value = $('option:selected',this).text();
like image 24
Satyam Khatri Avatar answered Sep 30 '22 21:09

Satyam Khatri


This should work:

$(this).find('option:selected').text();
like image 39
He Shiming Avatar answered Sep 30 '22 20:09

He Shiming


You can use find to look for the selected option that is a descendant of the node(s) pointed to by the current jQuery object:

var cur_value = $(this).find('option:selected').text();

Since this is likely an immediate child, I would actually suggest using .children instead:

var cur_value = $(this).children('option:selected').text();
like image 31
Platinum Azure Avatar answered Sep 30 '22 22:09

Platinum Azure


var cur_value = $(this).find('option:selected').text();

Since option is likely to be immediate child of select you can also use:

var cur_value = $(this).children('option:selected').text();

http://api.jquery.com/find/

like image 25
thomthom Avatar answered Sep 30 '22 22:09

thomthom