Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery getting all dropdown values

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?

like image 472
bba Avatar asked Oct 13 '10 12:10

bba


People also ask

How to select dropdown value using jQuery?

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.


2 Answers

How about something like:

var values = []; $('#my_dropdown option').each(function() {      values.push( $(this).attr('value') ); }); 
like image 179
VoteyDisciple Avatar answered Sep 24 '22 01:09

VoteyDisciple


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()

like image 31
jAndy Avatar answered Sep 23 '22 01:09

jAndy