views:

54

answers:

1

I want to fill an array in javascript that takes all the values out of different selectboxes with the same name

so for example if i have a html like this:

<select name="selectbox[]">
<option value="value1">text1</option>
<option value="value2">text2</option>
</select>

<select name="selectbox[]">
<option value="value1">text1</option>
<option value="value2">text2</option>
</select>

I have to get the selected values of both selectboxes and put it in an array.

what i did:

 $("input[name=selectbox\\[\\]]").map(function(){return $(this).val();});

does not work.

A similar example wich does work for textboxes:

$("input[name=textbox\\[\\]]").map(function(){return this.value;});

Thanks for your help!

+2  A: 

Perhaps because its a select and not an input ??

var $select = $("<select multiple='multiple' />");
for (var x=0; x<3; x++) {
  $select.append("<option value='"+x+"' selected='selected'>"+x+"</option>");
}
console.log($select.val());
//  ["0", "1", "2"]
console.log($select.map(function() { return $(this).val() }));
//  jQuery("0", "1", "2")

// create a second select:
$select = $select.clone(true).andSelf();
console.log($select.map(function() { return $(this).val() }));
//  jQuery("0", "1", "2", "0", "1", "2")
gnarf
oops, overlooked that one haha :D
Sobek