How to get all parameter values ​​(selected / not selected) in the selection field

I want to get all parameter values ​​(selected / unselected) in selectbox at the click of a button. How can i do this?

+7
jquery drop-down-menu
source share
3 answers
var arr = new Array; $("#selectboxid option").each ( function() { arr.push ( $(this).val() ); }); alert ( arr.join(',' ) ); 

press the button

  $("#btn1").click ( function() { var arr = new Array; $("#selectboxid option").each ( function() { arr.push ( $(this).val() ); }); alert ( arr ); }); 
+10
source share

I think this is a good opportunity to use the Traversing / map method:

 var valuesArray = $("#selectId option").map(function(){ return this.value; }).get(); 

And if you want to get two separate arrays containing selected and unselected values, you can do something like this:

 var values = { selected: [], unselected:[] }; $("#selectId option").each(function(){ values[this.selected ? 'selected' : 'unselected'].push(this.value); }); 

After that, arrays of values.selected and values.unselected will contain the correct elements.

+13
source share

err ok ..

 $('#selectbox').click(function() { var allvals = []; $(this).find('option').each(function() { allvals.push( $(this).val() ); }; }); 

or maybe you mean

 $('#thebutton').click(function() { var allvals = []; $('#theselectbox').find('option').each(function() { allvals.push( $(this).val() ); }; }); 
+3
source share

All Articles