Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
71
rated 0 times [  77] [ 6]  / answers: 1 / hits: 41272  / 14 Years ago, fri, august 13, 2010, 12:00:00

I need to get the value of the selected option (when changed) in a select list and change the text in a span next to the select list. The problem is I don't know the id of the select list. There are a lot of different select lists on the page (5-25+) and they are all created dynamically, and so I can't have the id specified in the .change(). Here is what I have:



JavaScript:



$(select).change(function () {
var str = ;
str = $(select option:selected).text();

$(.out).text(str);
}).trigger('change');


(Of course this doesn't work, puts all of the select values in each span.)



HTML:



<select name=animal[]>
<option value=dog>dog</option>
<option value=cat>cat</option>
<option value=bird>bird</option>
<option value=snake>snake</option>
</select>
<span class=out></span>


What am I missing?


More From » jquery

 Answers
23

Try something like this:



$(select).change(function () {
var txt = $(this).val();
$(this).next('span.out').text(txt);
}).trigger('change');​

[#95924] Wednesday, August 11, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
nikhilh

Total Points: 224
Total Questions: 89
Total Answers: 99

Location: Bahrain
Member since Fri, Sep 16, 2022
2 Years ago
;