Random Snippets

  • Home
  • Sequence analysis blog
  • About
  • Categories
    • javascript
    • mysql
    • php
  • Subscribe via RSS

JavaScript to select all or none of the checkboxes in a form

February 28th, 2008  |  Published in javascript  |  5 Comments


Here is a quick demo of the select all or none JavaScript function that automatically toggles all of your checkboxes in a given form.

Demo
My favorite programming/scripting language is:

Select All | None

JavaScript

Perl

PHP

C++


This is the plain HTML of the form:

1
2
3
4
5
6
7
8
<form name="theForm">
My favorite programming/scripting language is:<p>
Select <a href="javascript:selectToggle(true, 'theForm');">All</a> | <a href="javascript:selectToggle(false, 'theForm');">None</a><p>
<input type="checkbox" name="answers[]" value="javascript">JavaScript<p>
<input type="checkbox" name="answers[]" value="perl">Perl<p>
<input type="checkbox" name="answers[]" value="php">PHP<p>
<input type="checkbox" name="answers[]" value="c++">C++
</form>

There is nothing exciting about the HTML code except for line 3 where all the magic happens. There are 2 links in this line of code:

  • All - When the user clicks on this link, it will execute the selectToggle() JavaScript function and passes over 2 arguments: the value to set the checkboxes (true in this case) and the name of the form.
  • None - This link executes the selectToggle() JavaScript function and passes over the false value to uncheck the checkboxes and the name of the form.

Here is a peek at the selectToggle() JavaScript function:

1
2
3
4
5
6
7
8
9
10
11
function selectToggle(toggle, form) {
     var myForm = document.forms[form];
     for( var i=0; i < myForm.length; i++ ) { 
          if(toggle) {
               myForm.elements[i].checked = "checked";
          } 
          else {
               myForm.elements[i].checked = "";
          }
     }
}

This function basically loops through all of the inputs in the given form name that is passed over as an argument. Depending on the value of the toggle parameter, which can be true or false, each form element will checked or unchecked respectively.

Share with a friend:
    

Customize message


[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Responses

Feed Trackback Address
  1. rodrigo says:

    July 3rd, 2008 at 2:42 pm (#)

    This is a good script, all the others i had found check or uncheck by the name of the checkbox and not by the form name

    [Reply]

  2. All and None Checkbox function in javascript - Global Point Forum says:

    August 6th, 2008 at 6:06 am (#)

    [...] and None Checkbox function in javascript JavaScript to select all or none of the checkboxes in a form Here is a quick demo of the select all or none JavaScript function that automatically toggles all [...]

  3. Mark Iliff says:

    December 18th, 2008 at 7:02 pm (#)

    Simple, elegant and effective – so refreshing! Thanks for posting.

    ยต

    [Reply]

  4. Chris says:

    December 3rd, 2009 at 6:46 am (#)

    Old post but still very effective. This script works perfectly.

    [Reply]

  5. Harley says:

    April 1st, 2010 at 1:31 pm (#)

    Thank you so much, onclick scripts are confusing, and most do not work, I as a coder can understand this one, and it work perfectly.

    [Reply]

Comments or feedback...

If you have any demos that you would like to request, please do so.

Click to cancel reply

Recent Posts

  • Sorting 2D associative arrays in PHP
  • Dynamic or on-the-fly percentage calculations with JavaScript
  • The dangers of embedding the notorious “void(0)” JavaScript code in the href attribute of the “a” tag
  • How to randomly order or select rows in a MySQL query
  • How to convert MySQL timestamp to PHP date type

Recent Comments

  • Anders K on Simulate a button click via JavaScript
  • john on How to find and replace text dynamically via JavaScript
  • Eiolon on How to hide, show, or toggle your div
  • Use label as distant button in HTML – Community – travellin' meets real life on Simulate a button click via JavaScript
  • gaurav on Simulate a button click via JavaScript

Archives

Categories

  • javascript
  • mysql
  • php

©2010 Random Snippets