jQuery: Count the Number of Checkboxes Checked on a Page

If you develop websites and work with JavaScript, but have never heard of jQuery before, then it is probably best that you start reading up on it right now. After all, as they like to put it themselves, jQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development. jQuery is designed to change the way that you write JavaScript!

To return the number of checked or ticked checkboxes on a page using jQuery is in actual fact pretty simple.

By making use of the special :checked selector and combining it with the standard length property, you code snippet to retrieve the number of checked checkboxes on a page looks like this:

alert($(":checkbox:checked").length);

Similarly, if you wanted to locate all the checked checkboxes based on a specific selector grouping, you would make use of the .filter function and combine it with those we used above. So your code would now look as follows:

alert($('.createrightcheckbox').filter(":checked").length);

Note, if you wanted to run the same code above but only pick up on unchecked checkboxes, you could run:

alert($('.createrightcheckbox').filter(":not(:checked)").length);

Nifty.

Related Posts :

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 jQuery and tagged , , , , . Bookmark the permalink.
  • Mistycolours33

    really useful..helped me a lot

  • Mistycolours33

    I also wanted to know how can I delete multiple rows( by checking checkboxes) using a single “delete” button.

  • Pedro

    awessome!! simple and effective. One more question. How can I get the values of the checked ones??

  • Guest

    Lorem Ipsum is simply dummy text of the printing and
    typesetting industry. Lorem Ipsum has been the industry’s standard
    dummy text ever since the 1500s, when an unknown printer took a galley
    of type and scrambled it to make a type specimen book. It has survived
    not only five centuries, but also the leap into electronic typesetting,
    remaining essentially unchanged. It was popularised in the 1960s with
    the release of Letraset sheets containing Lorem Ipsum passages, and more
    recently with desktop publishing software like Aldus PageMaker
    including versions of Lorem Ipsum.