SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2001
    Location
    T.O., Canada
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Urgent Checkbox HELP needed!!

    I have a group of checkboxes named language (name='language[]'). It is an array to store zero or more languages:

    Code:
    	
    
    <script language="javascript">
    
    <!--
    function uncheckAny(field)
    {
    field[0].checked = false ;
    }
    
    function uncheckRest(field)
    {
    for(i=1; i<field.length; i++)
      field[i].checked = false ;
    }
    		
     -->
    </script>
    
    <form name='myform' method='post' action='<?php echo $PHP_SELF ?>'>  
    
    <input type='checkbox' class='form_objects' name='language[]' value='A' checked onClick="uncheckRest(window.document.myform.language);">Any<br>
    
    <input type='checkbox' class='form_objects' name='language[]' value='EN' onClick="uncheckAny(window.document.myform.language);">English <br>
    
    <input type='checkbox' class='form_objects' name='language[]' value='FR' onClick="uncheckAny(window.document.myform.language);">French <br>
     
      <!-- .....
      //edited for length  -->
    If I click on "Any", I want all other checkboxes to be unchecked. Similarly, if I click on a checkbox that's not "Any", I want "Any" to be unchecked.

    However I can't get the above code to work. When run in IE the error message said field[x] is NULL or not an object. I've tried changing from window.document.myform.language to window.document.myform.language[] ,but still won't work.

    Can someone see the problem? I've been trying to solve this problem for so long!!

  2. #2
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    check this out as well

    http://members.aol.com/grassblad/html/chkAllBut.html

    PHP Code:

    <script type="javascript">
    <!--
      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 ;
        }
      }

      function checking(field)
      {
        if (field[0].checked) {checkAll(field);}
        else                  {uncheckAll(field);}
      }
            
    // -->
    </script>
    <div id='nn'></div>
    <form name='myform' method='post' action='<?php echo $PHP_SELF ?>'>  
      <input type='checkbox' name='ling' value='A'  onClick="checking(this.form.ling);">select all<br />
      <input type='checkbox' name='ling' value='EN' onClick="" >English <br />
      <input type='checkbox' name='ling' value='FR' onClick="" >French  <br />
    </form>

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2001
    Location
    T.O., Canada
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Andrew thanks for the reply, but I don't think it's gonna work. In my code language is an array with the square brackets. I need it to be an array for php processing. I think the brackets are throwing javascript off and the reason why it's not working.

    Any idea to go around this problem?

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2001
    Location
    T.O., Canada
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I got it to work. I changed the "Any" checkbox to a different name, and then I use IndexOf() to check for checkboxes that starts with the name "language" or "lang_any", thereby bypassing the []:

    Code:
    <script language="javascript">
    
    <!--
    function uncheckAny(elm,name){
      for (var i = 0; i < elm.form.elements.length; i++){
      if (elm.form.elements[i].name.indexOf(name) == 0)
        elm.form.elements[i].checked = false;
    	}
    }
     
    function uncheckRest(elm,name){
      for (var i = 0; i < elm.form.elements.length; i++){
      if (elm.form.elements[i].name.indexOf(name) == 0)
        elm.form.elements[i].checked = false;
    	}
    }
    
     -->
    </script>
    
    <form name='myform' method='post' action='<?php echo $PHP_SELF ?>'> 
    
    <input type='checkbox' class='form_objects' name='lang_any' value='A' checked onClick="uncheckRest(this,'language');">Any<br>
    
    <input type='checkbox' class='form_objects' name='language[]' value='EN' onClick="uncheckAny(this,'lang_any');">English <br>
    
    <input type='checkbox' class='form_objects' name='language[]' value='FR' onClick="uncheckAny(this,'lang_any');">French <br>
    
      <!-- .....
      //edited for length  -->
    so now if "Any" is checked, all other checkboxes will be unchecked. And if any checkbox other than "Any" is checked, "Any" will be unchecked.


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
  •