0

I have following code to get the values of all checked checkboxes. Surprisingly last element of an array comes as a 'Array'.

var selected = [];
$('#checkboxes input:checked').each(function(){
  selected.push($(this).attr('value'));
});

Even if only one checkbox is checked, it adds extra element in an array. Array will be like this:

selected[0]=Dove
selected[1]=Array

What can be the issue with it? I'm unable to find any reason behind this. Can anyone help?

HTML Code

<ul id='checkboxes' class="list-style1">
 <?php foreach($brands as $row){ ?>
 <span class='checkbox-wrapper' id='<?php echo $brand; ?>'>
 <li><input type='checkbox' value='<?php echo $row['brand']; ?>'>
 <label for='<?php echo $row['brand']; ?>'><?php echo $row['brand']; ?></label>
 </li></span>
 <?php } ?>
</ul>
Gui Imamura
  • 556
  • 9
  • 26
Guy in the chair
  • 1,045
  • 1
  • 13
  • 42

1 Answers1

1
var checkedValues = $('#checkboxes  input:checked').map(function() {
return this.value;
}).get();

it will return the selected value of the checkbox in array.

referrence link

Community
  • 1
  • 1
asimshahiddIT
  • 330
  • 2
  • 5