jQuery: check all checkboxes


I have a form with three different fields. Each field is an array made of checkboxes. I’d like to have three “checkall” checkboxes, one for each field, so that you only click once to select every checkbox of the array (or rather of what will be treated as an array server side).

How would you do that? Could you point me to a possible scenario? I’ve spent a few hours on this problem without reaching any interesting result…


-jj. :slight_smile:

Because your using array names it would be easier to use regular javascript, here is a code I’ve used for a long time for check box arrays

function checkAll(name,checked){
    var e = document.getElementsByTagName('input');
    for(var i=0, maxi=e.length; i<maxi; i++){
        if (e[i].name == name+'[]'){
            e[i].checked = checked;

Then for the HTML its as simple as

<a href="javascript:void(0)" onclick="checkAll('mycheckbox',true)">Check All</a> - <a href="javascript:void(0)" onclick="checkAll('mycheckbox',false)">Uncheck All</a>

If you do want to use jQuery then simply add a class to each element and use the following to call it

function checkAll(name,checked){
    $('.'+name).attr('checked', checked);