2

Please advice, how to find selected value of a html select on its change.

code is below

$(".js-CellFirst").change(function () { alert($(this.find('option:selected')).text());<-- **trouble line** }); 

thanks

1

2 Answers 2

3

To get the value...

$(".js-CellFirst").val(); 

To get the text of the selected option element.

$(".js-CellFirst option:selected").text(); 

However, your specific issue is with your parenthesis, of which you have nested correctly. The correct use is...

alert($(this).find('option:selected').text()); 
Sign up to request clarification or add additional context in comments.

9 Comments

The jQuery library provides .val() for a reason: text() and others have some browser inconsistency. The val method works for nearly any object in jQuery.
@Levi Browser inconsistency is a strong reason why jQuery exists. I don't understand what your comment is getting at...
@alex Don't use .text(), use .val(). Text only works on certain types of elements, but val works on nearly all. That's all I'm saying.
@Levi To get an element's text node?
|
2

Get selected value of a dropdown's item using jQuery

Just use alert($(this).val(); instead:

$(".js-CellFirst").change(function () { alert($(this).val()); }); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.