views:

43

answers:

5

I have div with id=”content”

The div may contain a lot of

<input id="chkboxtag1" type="checkbox" value="1"/>

Div may contain ul tag. This ul may also contain check boxes. The div may contain divs or ul, it may also contain checkboxes.

Is there any chance that I get values of all checked check boxes inside the div (not necessarily immediate inside the div)?

+4  A: 
$('input[type="checkbox"]:checked').each(function(){
  alert($(this).val())
})

By this way, all checked checkboxes on you page will be "alerted" on screen.

See the jQuery Selectors doc

André Gadonski
This doesn't answer the question, this gets **all** checkboxes on the screen...
Nick Craver
+1  A: 

Use the following:

var values = [];
$('#content input[type="checkbox"]:checked').each(function() {
    values.push($(this).val());
});
elusive
+2  A: 
var selectedItems = new Array();

$('#content input:checkbox:checked').each(function() {
       selectedItems.push($(this).val());
  });
Clicktricity
+1  A: 

You can use .map() for this:

var arrayOfValues = $('#content input:checkbox:checked').map(function() {  
                      return this.value; 
                    }).get();

This gets an array of the values for the checked checkboxes under <div id="content">, you can use it as an array, or .join(', ') to get a string...whatever use you want really.

Nick Craver
+1  A: 

All above are answeres i thought only immediate child is concodered but Jquery is great!