It seems that you really want it, this is a common control that associates a select box with its div div tag. To do this, you need a little different:
http://jsfiddle.net/b9chris/KpUWP/
(function() { function update() { var str = $.map($('option:selected', this), function(option) { return $(option).text(); }).join(' '); $(this).next().text(str); }; $("select").change(update) .each(update); })();
So, first you define the update function separately, instead of assigning it directly to the change () handler, and then throwing the change () event. Although this is a very good hack, it introduces strange problems if something else needs to be heard for the change event.
Then, to get the div that you want to assign to your line, since you know that this is always the next brother, you simply use .next() .
To create the string separator string that you built, instead of each loop, you can simply use the map function, which simply takes each item in the selected list (in this case, the selected option tags) and returns their text value. The result is a simple array of what you wanted .Jin's built-in .join () method to build your final string from.
You seem to have noticed that this object in the update function is a select tag; you can use this as a search context, for example, calling $('option:selected', this) to get only those parameters of this tag, instead of resorting to identifiers and trying to work out what the selected handler refers to.
source share