Thursday, April 25, 2024
 Popular · Latest · Hot · Upcoming
96
rated 0 times [  101] [ 5]  / answers: 1 / hits: 18042  / 13 Years ago, fri, april 15, 2011, 12:00:00

EDIT: The HTML and js bellow is a simplified version. Check out the jsfiddle link on the bottom of my post for full demonstration of my problem.






I have a select HTML element:



<select name=foo id=foo>
<option value=1>a</option>
<option value=2>b</option>
<option value=3>c</option>
</select>


I want to get the label of a selected option with jQuery. This, however:



alert($(#foo option:selected).text());


Returns:



a
b
c


I want to get just, for example:



b


jsfiddle: http://jsfiddle.net/8KcYY/1/ (click on the Vybrať značku button).


More From » jquery

 Answers
10

This works:



<select name=foo id=foo>
<option value=1>a</option>
<option value=2>b</option>
<option value=3>c</option>
</select>
<input type=button id=button value=Button />

$('#button').click(function() {
alert($('#foo option:selected').text());
});


Try it yourself: http://jsfiddle.net/Nyenh/



Even simpler:



$('#foo').change(function(){
var selected = $(this).find('option:selected');
alert(selected.val() + ' ' + selected.text());
});


http://jsfiddle.net/qtRhQ/1/


[#92709] Thursday, April 14, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
marcos

Total Points: 331
Total Questions: 106
Total Answers: 104

Location: Gabon
Member since Sat, Jul 25, 2020
4 Years ago
marcos questions
Thu, Feb 24, 22, 00:00, 2 Years ago
Sat, Apr 17, 21, 00:00, 3 Years ago
Mon, Feb 1, 21, 00:00, 3 Years ago
Tue, Jan 26, 21, 00:00, 3 Years ago
Wed, Jan 6, 21, 00:00, 3 Years ago
;