I need a JQuery help..
i have populated checkboxes as follows
<span class="checked">
<input class="cbid" type="checkbox" name="cb1">
</span>
<span >
<input class="cbid" type="checkbox" name="cb2">
</span>
<span class="checked">
<input class="cbid" type="checkbox" name="cb3">
</span>
i need to make an array using the value of name attribute of checkboxes where their parent's(span) class is 'checked'
e.g.
["cb1","cb3"]
Thanks
var names = $('.checked > :checkbox').map(function() {
return this.name;
}).toArray();
Fiddle
Using a class selector in conjunction with the child selector (>) and the jQuery (Sizzle) :checkbox selector (which is equivalent to [type=checkbox]).
$().map() returns a jQuery object, hence the .toArray() at the end. This is just one of the many ways to achieve the requested result.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With