Short code execution in excerpts

I need jquery help.

I set the 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 create an array using the value of the name attribute of the flags, where the parent class (span) is 'checked'

eg.

["CB1", "CB3"]

thank

+3
source share
1 answer
var names = $('.checked > :checkbox').map(function() {
    return this.name;
}).toArray();

Fiddle

Using the class selector in conjunction with the child selector ( >) and the jQuery (Sizzle) selector :checkbox(which is equivalent [type=checkbox]).

$().map()returns a jQuery object, hence .toArray()at the end. This is just one of many ways to achieve the requested result.

+4
source

All Articles