views:

312

answers:

4

Hi All,

I want to fetch all the option values(selected/unselected) in a selectbox on click of a button. Is this possible..?? If it is a yes how can i achieve that..??

Thanks in advance

+2  A: 

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() ); };
});
Scott Evernden
+2  A: 
var arr = new Array;

    $("#selectboxid option").each  ( function() {
       arr.push ( $(this).val() );
    });

alert ( arr.join(',' ) );

in the button click

    $("#btn1").click ( function() {
        var arr = new Array;
        $("#selectboxid option").each ( function() {
            arr.push ( $(this).val() );
        });
        alert ( arr );
    });
rahul
A: 
var sel = new Array();
var uns = new Array();

$('#select option').each(function(i, value){
    if ($(value).attr('selected') == 'selected')
       sel[] = $(value).val();
    else
       uns[] = $(value).val();
});
yoda
+3  A: 

I think 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 the 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, the values.selected and values.unselected arrays will contain the right elements.

CMS