Reputation: 22556
I am trying to get a list of strings with all the values of the
Here is my html:
<label>Select Roles</label>
<span id="dualselect" class="dualselect">
<select name="RolesSelect" id="dualSelectRoles1" multiple="multiple" size="10">
<option value="">Avaliable Roles</option>
</select>
<span class="ds_arrow">
<span class="arrow ds_prev">«</span>
<span class="arrow ds_next">»</span>
</span>
<select name="select4" multiple="multiple" id="dualSelectRoles2" size="10">
<option value="12">Users Roles</option>
</select>
</span>
Here is my JS:
<script>
function submitForm() {
var arr = new Array;
$("#dualSelectRoles2").each(function () {
arr.push($(this).val());
});
alert(arr);
}
</script>
My alert is displaying nothing?
Upvotes: 0
Views: 49
Reputation: 17755
You need to reference the option elements, then iterate over them:
var arr = new Array;
$("#dualSelectRoles2 option").each(function () {
arr.push(this.value);
});
alert(arr);
Also, you can replace $(this).val()
with this.value
. You don't need a jQuery object here.
Upvotes: 1
Reputation: 9351
DEMO : http://jsfiddle.net/qsDn5/22/
you need to run loop through each option.
var arr = new Array;
$("#dualSelectRoles2 option").each(function () {
arr.push(this.value);
});
alert(arr);
Upvotes: 0
Reputation: 82241
It should be:
var arr = new Array;
$("#dualSelectRoles2 option").each(function () {
arr.push($(this).val());
alert(arr);
});
Upvotes: 1
Reputation: 388316
You need to find the option elements within the select, then use .map()
function submitForm() {
var arr = $("#dualSelectRoles2 option").map(function () {
return this.value
}).get();
alert(arr);
}
Upvotes: 0