txtfield.value = $(lb.options).map(function() { return this.value; }).get().join(',');
This one uses .map() to create a jQuery object, returning the value each option , then uses .get() to retrieve the array from the object.
EDIT: As @Nick Craver noted in the comment below, if you need to get optimal performance (with jQuery), it makes more sense to use the $.map() option since you already have the collection. See his answer for more details .
EDIT: For best performance, do a for loop, but cache property references. It matters .
var arr = [], opts = lb.options, len = opts.length; for (var i = 0; i < len; i++) { arr[i] = opts[i].value; } txtfield.value = arr.join(',');
user113716
source share