SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast mistafeesh's Avatar
    Join Date
    Sep 2002
    Posts
    55
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    check all boxes script not working!

    Please help a JavaScript noob debug his code...ta.

    I'm trying to use this script to make a button which ticks all tickboxes in a group.

    Here's the scripty thing from the top o' the page:

    Code:
    <SCRIPT language="JavaScript">
    <!--    
    // by Nannette Thacker
    // http://www.shiningstar.net
    // This script checks and unchecks boxes on a form
    // Checks and unchecks unlimited number in the group...
    // Pass the Checkbox group name...
    // call buttons as so:
    // <input type=button name="CheckAll"   value="Check All"
            //onClick="checkAll(document.myform.list)">
    // <input type=button name="UnCheckAll" value="Uncheck All"
            //onClick="uncheckAll(document.myform.list)">
    // -->
    
    <!-- Begin
    function checkAll(field)
    {
    for (i = 0; i < field.length; i++)
            field[i].checked = true ;
    }
    
    function uncheckAll(field)
    {
    for (i = 0; i < field.length; i++)
            field[i].checked = false ;
    }
    //  End -->
    </script>
    which I just put underneath the other JS bits generated by dreamweaver.

    then here's the code for the actual button and checkboxes(one of the sets):
    Code:
    <input type="button" name="South Hams" value="South Hams" onClick="checkAll(document.form1.4)" >
                
        <input type="checkbox" name="4" value="8">
        Totnes |&nbsp;
            
        <input type="checkbox" name="4" value="9">
        Kingsbridge |&nbsp;
            
        <input type="checkbox" name="4" value="10">
        Modbury |&nbsp;
            
        <input type="checkbox" name="4" value="11">
        Dartmouth |&nbsp;
            
        <input type="checkbox" name="4" value="12">
        Ivybridge
    It just doesn't work at all. any idea what I'm doing wrong?
    we are all in the gutter, but some of us are looking at the stars

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    '4' is not a legal name/identifier. You could get away with it by calling the function as checkAll(document.form1['4']) - but, since an HTML form submits data to the server as name/value pairs, '4 = 8' seems an odd way of specifying information (unlike, say, 'team2 = 8'). Try something with letters, underscores, and/or numbers - but not beginning with a number.
    ::: certified wild guess :::

  3. #3
    American't awestmoreland's Avatar
    Join Date
    Sep 2002
    Location
    Grand Rapids, MI
    Posts
    1,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mistafeesh
    ...any idea what I'm doing wrong?
    A couple of things worth mentioning:
    1. Your onClick statement refers to "document.form1.4" but the code you pasted above doesn't contain a form (see red edit below).
    2. The name given to your checkboxes is "4". I'm sure that the name can't begin with a number and it almost certainly needs to begin with a letter. I've changed the name to "A" (see blue edit below).
    Code:
    	<form name="form1">
    	  <input type="button" name="South Hams" value="Check all" onClick="checkAll(document.form1.A)" />
    			  
    	  <input type="checkbox" name="A" value="8" />
    	  Totnes |&nbsp;
    		  
    	  <input type="checkbox" name="A" value="9" />
    	  Kingsbridge |&nbsp;
    		  
    	  <input type="checkbox" name="A" value="10" />
    	  Modbury |&nbsp;
    		  
    	  <input type="checkbox" name="A" value="11" />
    	  Dartmouth |&nbsp;
    		  
    	  <input type="checkbox" name="A" value="12" />
    	  Ivybridge 
    	</form>
    Here's a working DEMO so that you can see it in action.


    Andy
    From the English nation to a US location.

  4. #4
    American't awestmoreland's Avatar
    Join Date
    Sep 2002
    Location
    Grand Rapids, MI
    Posts
    1,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Pre-empting your next question, add this line to remove all checks
    Code:
    <input type="button" name="Not South Hams" value="Uncheck all" onClick="uncheckAll(document.form1.A)" />
    Demo in previous post above has been updated to include this.


    Andy
    From the English nation to a US location.

  5. #5
    SitePoint Enthusiast mistafeesh's Avatar
    Join Date
    Sep 2002
    Posts
    55
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    thanks guys/gals. The code is within a form - just quite a complex one, so I just pasted across that little bit.

    changing the '4' for a word did it!! thanks again.
    we are all in the gutter, but some of us are looking at the stars


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
  •