SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2005
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Check/Uncheck All

    I'd like the ability to have a checkbox that will check all elements with a certain name when selected, and to have another checkbox that will do the same with another set of elements.

    I had found the following code, and it worked:

    Code:
    function toggleChecked(oElement) 
    { 
      oForm = oElement.form; 
      oElement = oForm.elements[oElement.name]; 
      if(oElement.length) 
      { 
        bChecked = oElement[0].checked; 
        for(i = 1; i < oElement.length; i++) 
          oElement[i].checked = bChecked; 
      } 
    }
    
    function toggleController(oElement)
    {
    oForm=oElement.form;oElement=oForm.elements[oElement.name];
    if(oElement.length)
    {
    bChecked=true;nChecked=0;for(i=1;i<oElement.length;i++)
    if(oElement[i].checked)
    nChecked++;
    if(nChecked<oElement.length-1)
    bChecked=false;
    oElement[0].checked=bChecked;
    }
    }
    I was able to the use the following HTML for my check/uncheck checkboxes:

    <input type="checkbox" name="addsuggestedjokes[]" onclick="toggleChecked(this)" />
    <input type="checkbox" name="removesuggestedjokes[]" onclick="toggleChecked(this)" />

    And the following for my other checkboxes:

    <input type="checkbox" name="addsuggestedjoke[]" value="<?php print $jokeid ?>" onclick="toggleController(this)" />
    <input type="checkbox" name="removesuggestedjoke[]" value="<?php print $jokeid ?>" onclick="toggleController(this)" />

    However, this presented a problem because I do not want my check/uncheck checkboxes to be part of my addsuggestedjoke[] or removesuggestedjoke[] arrays!

    I tried to change everything as follows, but it doesn't work:

    Code:
    Changed toggleChecked:
    
    function toggleChecked(oElement, elementName) 
    { 
      oForm = oElement.form; 
      oElement = oForm.elements[elementName]; 
      if(oElement.length) 
      { 
        bChecked = oElement[0].checked; 
        for(i = 1; i < oElement.length; i++) 
          oElement[i].checked = bChecked; 
      } 
    }
    Changed check/uncheck HTML:

    <input type="checkbox" name="addallsuggestedjokes" onclick="toggleChecked(this, 'addsuggestedjoke[]')" />
    <input type="checkbox" name="removeallsuggestedjokes" onclick="toggleChecked(this, 'removesuggestedjoke[]')" />

    How can I make this work?

  2. #2
    SitePoint Addict richtestani's Avatar
    Join Date
    Nov 2003
    Location
    Bridgeport
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not toally clear on this, but I'm going to take a stab.

    The changed HTML will not work since you are passing too many parameters.

    Secondly, because you have some php in there, I will assume some dynamic creation. Set the 2 seperate checkboxes apart from your list and do not give them a name and pass the names of the set you want to change. I think it should work like this.

    Code:
    <input type="checkbox" name="removeallsuggestedjokes" onclick="toggleChecked('removesuggestedjoke')" />
    You could also set this into a link which may seem more user friendly and easier to integrate.
    Code:
    <a href="#" onclick="toggleChecked('removesuggestedjoke');">Toggle Checks</a>
    RichTestani
    -------------------------------
    http://www.junkdepot.com
    http://www.rareoopdvds.com | The Movie Poster Site

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2005
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I changed everything in bold: I changed the JavaScript and the HTML. The JavaScript now takes two parameters. I had to do this because "this" still needs to be passed to the function in order for the function to access oElement.form. So the way you suggest doesn't work.

    Anyone else?


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
  •