jQuery: Checking or Unchecking All Checkboxes

Providing functionality to check all, uncheck all or even to invert the checkboxes currently selected on a page is pretty simple to achieve with either plain old JavaScript or the newer, more flashy jQuery library. Important too if you think just how much more usable your site becomes with these handy shortcuts available to the user.

Now to select all checkboxes on a page and then check them, one simply has to make use of the handy INPUT[type='checkbox'] jQuery selector call and then follow that up with an attr call in order to change the checked attribute status to true. Finally, you bundle this all up and apply it to the click event of some handy button or span and end up with this:

$('#selectall').click(function(){
        $("INPUT[type='checkbox']").attr('checked', true);
});

Similarly to untick all checkboxes on your page you would go with something like this:

$('#selectnone').click(function(){
        $("INPUT[type='checkbox']").attr('checked', false);
});

The final little piece of magic on display for today is the function to invert the current checkbox tick selection. Basically here we are going to scroll through each checkbox using jQuery’s handy each function and set the checked attribute accordingly based on its currently held value, which in code would look something like this:

$('#selectinvert').click(function(){
        $("INPUT[type='checkbox']").each(function(){
                if (this.checked == false) {
                        this.checked = true;
                } else {
                        this.checked = false;
                }
        });
});

Deceptively simple, was it not? :P

You might also enjoy:

About Craig Lotter

Craig Lotter is an established web developer and application programmer, with strong creative urges (which keep bursting out at the most inopportune moments) and a seemingly insatiable need to love all things animated. Living in the beautiful coastal town of Gordon's Bay in South Africa, he games, develops, takes in animated fare, trains under the Funakoshi karate style and for the most part, simply enjoys life with his amazing wife and daughter. Oh, and he draws ever now and then too.
This entry was posted in Technology & Code, Tutorials and tagged , , , , , , , , , , . Bookmark the permalink.
  • http://dascritch.net Da Scritch

    Replace
    if (this.checked == false) {
    this.checked = true;
    } else {
    this.checked = false;
    }
    by
    this.checked = !this.checked ;

    simplier, faster, smart

  • http://www.craiglotter.co.za Craig Lotter

    Absolutely agree with you Da Scritch, far more efficient, but in terms of laying out exactly what is happening in terms of logic for more the more junior programmers out there, I prefer laying everything out in a nice and verbose manner so that they can follow step by step.

    Thanks for the snippet though! :)

  • http://www.craiglotter.co.za Craig Lotter

    Absolutely agree with you Da Scritch, far more efficient, but in terms of laying out exactly what is happening in terms of logic for more the more junior programmers out there, I prefer laying everything out in a nice and verbose manner so that they can follow step by step.

    Thanks for the snippet though! :)

blog comments powered by Disqus