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!

1 Answer 1

2

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")
Sign up to request clarification or add additional context in comments.

1 Comment

oops, overlooked that one haha :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.