Tech TLDR;

  • Archive
  • Top Posts
  • GitHub
  • LinkedIn
  • Contact

Toggle All Checkboxes Using jQuery

March 8, 2013 by admin

I needed to perform a mundane task of going to a large number of pages, with a large number of check-boxes selected at random, and flipping them in the opposite order.

So if I had 3 check-boxes, out of which two were selected like this:

I would need to flip them so it would look like this:

The 3 lines of code bellow, would allow me to do just that in the console window of chrome debugger tools.

$(':checkbox').each(function(){
   this.checked = !this.checked;
});Code language: JavaScript (javascript)
  1. $(':checkbox') selects all check-boxes on the page
  2. .each( iterate through each check-box and
  3. this.checked = !this.checked; changes the status of each check-box to opposite of what it originally was.

Just another example of how powerful jQuery can be. Pretty cool stuff!

P.S You can’t use $ in WordPress, you have to use keyword jQuery instead. So if you are tempted to try the above code on this page, use the following instead.

jQuery(':checkbox').each(function(){
   this.checked = !this.checked;
});Code language: JavaScript (javascript)

Filed Under: Front End, jQuery Tagged With: jQuery

Copyright © 2025 · eleven40 Pro Theme on Genesis Framework · WordPress · Log in