jQuery: Select All Checkbox

Posted by Jetlogs @ 11:52 pm
Category: jQuery,Web Development

Note: [@attr] selectors are no longer supported by jQuery 1.3.x and above. Use [attr] instead

Here is a simple tutorial on how to implement the “select all” function for checkboxes in jQuery. All you need to know for this tutorial is the basic knowledge of using the jQuery library.

assuming we have included the jQuery library in our document:


<script type="text/javascript" src="jquery.js"></script>

The next step would be defining our form. For our example, we’ll just use a very simple checkbox list to select which paradigms are applicable to Javascript. We’ll name these checkboxes simply as “paradigm”:


<input type="checkbox" name="paradigm"
value="Imperative">Imperative<br>
<input type="checkbox" name="paradigm"
value="Object-Oriented">Object-Oriented<br>
<input type="checkbox" name="paradigm"
value="Functional">Functional<br>

and of course, we need the “select all” checkbox to perform the operation. We’ll give this checkbox an id name of paradigm_all:


<input type="checkbox" id="paradigm_all">Select All

Now lets go to the main part of jQuery code. What happens when we select the “select all” checkbox is that the checkbox’s status will be passed to all of the checkbox list. For example, if the “select all” checkbox is checked, all checkboxes should also be checked, and vice versa.

So how do we select all of the checkboxes to change their checked state? Fortuantely, jQuery has XPath style selectors which allows us to choose multiple elements in the DOM. the expression:

input[@name=paradigm]

will select ALL input elements that are named paradigm. Here is how the final jQuery script should look like:


$(document).ready(function()
		{
			$("#paradigm_all").click(function()
			{
				var checked_status = this.checked;
				$("input[@name=paradigm]").each(function()
				{
					this.checked = checked_status;
				});
			});
		});

If you want to view a demo of the following script or to download the demo source code, just follow the links below.

View Demo
Download Source Code


12 Comments »

12 Comments to “jQuery: Select All Checkbox”

  1. chris
    1

    i found that on IE7 the “change” event wasn’t registering untill i clicked away from the checkbox.

    the “focus” event worked as expected.

  2. Jetlogs
    2
    Author Comment

    You are right, the “change” event didn’t work as expected on IE7. it seems that the “click” event is the proper way to do it.

    Edited the article to work on IE

  3. Trackback:
    3
    Mircea Soaica

    Select all checkboxes using jQuery…

    If you ever need to select all checkboxes in a form this tutorial will show you how to do it. This is useful when a user needs to delete all data in his control panel (emails, friends, articles etc.).

    All we need is a JavaScript function called Toggle…

  4. Homer
    4

    Try with the [] as the name of the checkbox!!!

    eg:

  5. Dylan Baxter
    5

    Note: In jQuery 1.3 [@attr] style selectors were removed (they were previously deprecated in jQuery 1.2). Simply remove the ‘@’ symbol from your selectors in order to make them work again. (http://docs.jquery.com/Selectors)

  6. Trelis Coriolis
    6

    How can you select them all with jquery when the checkboxes must have different names?

    eg
    Imperative
    Object-Oriented

  7. Many
    7

    Caution! As of Jquery 1.3.x @ isn’t supported anymore, just remove it.

    So line 6 should read:

    $(“input[name=checkall]“).each(function() {

  8. Jetlogs
    8
    Author Comment

    Thanks Many, gonna need to clean up some articles made during 1.2 and below

  9. Amy Wake
    9

    Here is a rewritten jQuery code above for jQuery 1.3.x:

    $(document).ready(function()
    {
    $("#paradigm_all").click(function()
    {
    var checked_status = this.checked;
    $("input[name=paradigm]").each(function()
    {
    this.checked = checked_status;
    });
    });
    });

    Found this jquery and checkbox article useful.

  10. techtricky
    10

    we can use the attribute “checkd” instead of loop all elements.

    $("#paradigm_all").click(function()
    {
    $("input[name=paradigm]").attr('checked',$(this).attr('checked'));

  11. techtricky
    11

    we can use the attribute “checkd” instead of loop all elements.

    $("#paradigm_all").click(function()
    {
    $("input[name=paradigm]").attr('checked', $(this).attr('checked'));

    }

  12. dhanesh mane
    12

    here is wht i m using

    $(“#divcontainers”).find(“:checkbox”).each( function() {
    // access each using $(this)
    });

    // select only checkboxes which are not checked
    $(“#divcontainers”).find(“:checkbox:not(‘:checked’)”).each( function() {
    // access each using $(this)
    });

    enjoy
    Dhanesh Mane

RSS Comment Feed Comments RSS |trackback TrackBack URI

Leave a comment

  • Archives

  • Donations

  • Social Bookmarks

  • Jetlogs.org
    Some Rights Reserved 2007
    Creative Commons License