I have few select boxes with same class. But when I use jQuery array push method to get all selected values in that class as an array it simply adds a comma (,) before each values. It works fins with static values. Issue is when I add values to select box from a PHP array. Here is how my select boxes code looks like
<select name="add_purch_item1" class="add_purch_item">
<option value="">----select----</option>
<?php if(!empty($pro_list1)) { foreach($pro_list1 as $products) { ?>
<option value="<?php echo $products->sku; ?>"><?php echo $products->product_name; ?></option>
<?php } } ?>
</select>
<select name="add_purch_item2" class="add_purch_item">
<option value="">----select----</option>
<?php if(!empty($pro_list2)) { foreach($pro_list2 as $products) { ?>
<option value="<?php echo $products->sku; ?>"><?php echo $products->product_name; ?></option>
<?php } } ?>
</select>
And I have added a click function to a button. When clicks it will get all selected values as an array. here is my jQuery code,
var selected = [];
$('.add_purch_item').each(function () {
selected.push($(this).val());
});
alert(selected);
And when I try select box with static values it gives correct output. See here an example
I am getting this wrong output in dynamic values. That unwanted comma is my issue. How to avoid that? I have tried string replace too.