I have dropdown <select>
with id my_dropdown
. I allow multi-select. I know how to pull the selected value(s) using jquery:
var values = $('#my_dropdown').val();
This will return an array of values if I select multiple. I also need to get all the values in the dropdown regardless of what is selected. How can I use jquery similarly to get all the values in the dropdown given that id?
Projects In JavaScript & JQuery With jQuery, it's easy to get selected text from a drop-down list. This is done using the select id. To change the selected value of a drop-down list, use the val() method.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With