Results 1 to 2 of 2

Thread: Select/unselect all checkbox fields using pure Javascript and HTML

  1. #1
    Join Date
    Feb 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Select/unselect all checkbox fields using pure Javascript and HTML

    The following code allows you to select multiple fields without having to select each checkbox one by one. This code is especially useful if you're developing a form or a CMS that allows you to delete and/or modify several items with one click. Enjoy:


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title> Select All </title>

    <meta http-equiv="Content-type" value="text/html; charset=UTF-8" />

    <script type="text/javascript" language="javascript">
    function SetAllCheckBoxes(FormName, AreaID, CheckValue)
    {
    if(!document.forms[FormName])
    return;
    var objCheckBoxes = document.getElementById(AreaID).getElementsByTagName('input');
    if(!objCheckBoxes)
    return;
    var countCheckBoxes = objCheckBoxes.length;
    if(!countCheckBoxes)
    objCheckBoxes.checked = CheckValue;
    else
    for(var i = 0; i < countCheckBoxes; i++)
    objCheckBoxes[i].checked = CheckValue;
    }
    </script>


    </head>

    <div id="items">
    <form name="SelectedItems" action="" method="post">
    <b>Select/Unselect All</b><br>
    <div style="background:#ccc;"><input name="checkall" type="checkbox" onclick="SetAllCheckBoxes('SelectedItems','items',this.checked);" /></div><br><br>

    <input type="checkbox" name="selected" value="Some text description 1"><input type="hidden" name="selecteditems" value=" "><br>
    <input type="checkbox" name="selected" value="Some text description 2"><input type="hidden" name="selecteditems" value=" "><br>
    <input type="checkbox" name="selected" value="Some text description 3"><input type="hidden" name="selecteditems" value=" "><br>
    <input type="checkbox" name="selected" value="Some text description 4"><input type="hidden" name="selecteditems" value=" "><br>
    <input type="checkbox" name="selected" value="Some text description 5"><input type="hidden" name="selecteditems" value=" "><br>
    <input type="checkbox" name="selected" value="Some text description 6"><input type="hidden" name="selecteditems" value=" "><br>
    </div>
    </form>

    </body>
    </html>

  2. #2
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default

    Very good script. It seems to work very well in all browsers (couldn't see where a problem would be, but you never know). People ask for stuff like that every now and again, and it will be nice to be able to simply point them here. Good work, and please keep it up!
    --Jas
    function GreatMinds(){ return "Think Like Jas"; }
    I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •