jquery getting all dropdown values jquery getting all dropdown values jquery jquery

jquery getting all dropdown values


How about something like:

var values = [];$('#my_dropdown option').each(function() {     values.push( $(this).attr('value') );});


Looks like:

var values = $('#my_dropdown').children('option').map(function(i, e){    return e.value || e.innerText;}).get();

See this in action: http://www.jsfiddle.net/YjC6y/16/

Reference: .map()


Example: http://jsfiddle.net/FadHu/

var opts = $('#my_dropdown')[0].options;var array = $.map(opts, function( elem ) {    return (elem.value || elem.text);});

The <select> element has a list of the options in its options property. Then use $.map(), which returns an array, to get either the option's value or text property.

(Note the $.map() behaves a little differently from $(element).map(). Can be a little tricky.)