SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist jazztie's Avatar
    Join Date
    Mar 2001
    Location
    the Netherlands
    Posts
    519
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    checking multiple checkboxes at once?

    I have the following problem...

    I have a list of names with checkboxes in front of them. The list is generated from a database.
    So in a loop it would look something like this:

    <input type=checkbox value='$userID' name='recipients[]'> $UserName

    The name of the checkbox is a Array, because I want to pass the information on to another page, where the emails-addresses are used to send a mail.

    the userID is also linked with the email-account of the user. What I want to do is to have a link, button or checkbox (which one is the easiest to implement) that checks all the checkboxes in the list at once. How do I go about this?

    I have come up with the following, but this doesn't seem to work:
    PHP Code:
    function CheckEm(cb_mail){
      for (var 
    0document.myForm[cb_mail].lengthi++)
        
    document.myForm[cb_mail][i].checked true;
    }

    <
    input type=checkbox onClick="CheckEm('cb_mail')"
    Can some help with this one? All help will be greatly appreciated

    Jazz
    Last edited by jazztie; Aug 4, 2001 at 14:37.

  2. #2
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow you came up with that yourself? Good work.
    ck :: bringing chris to the masses.

  3. #3
    SitePoint Evangelist jazztie's Avatar
    Join Date
    Mar 2001
    Location
    the Netherlands
    Posts
    519
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry Anarchos, you're right, you posted it first... I changed to question a bit while trying.

    The function I use now is:
    PHP Code:
        <!--
            function 
    CheckEm(recipients){
            if (
    document.mailer.cb_all.checked == true){
              for (var 
    0document.mailer[recipients[]].lengthi++)
                
    document.mailer[recipients][i].checked true;
            }
            else {
              for (var 
    0document.mailer[recipients[]].lengthi++)
                
    document.mailer[recipients][i].checked false;
            }
        }
        
    //--> 
    and in the form it reads:
    PHP Code:
    <form name='mailer' method='POST' action='mail_multiple_1.php'>
        <
    input type=checkbox name='cb_all' onClick=\"CheckEm('recipients')\"><br>

    $RelationQuery    = mysql_query("SELECT FROM relation");
    if(mysql_num_rows(
    $RelationQuery) > 0) {
      while (
    $rows    = mysql_fetch_array($RelationQuery)){
          extract(
    $rows);
        print "
    <tr><td><input type=checkbox value='$rid' name='recipients[]'>$firstName $lastName</td><td>$email</td></tr>";
      }

    I get an error, reading that 'Object is expected'.
    What am I doing wrong?

    PS: thanks goes out to Anarchos for writing the first part of the code!!!

  4. #4
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No problem, here's some code that works:
    <script type="text/javascript">
    <!--
    function checkEm(elm,name){
    for (var i = 0; i < elm.form[name].length; i++)
    elm.form[name][i].checked = elm.checked;
    }
    //-->
    </script>
    <form>
    <input type="checkbox" name="blah" />
    <input type="checkbox" name="blah" />
    <input type="checkbox" name="blah" />
    click <input type="checkbox" name="blah2" onClick="checkEm(this,'blah')" />
    </form>
    ck :: bringing chris to the masses.

  5. #5
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's a better version:
    Code:
    function checkAll(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 = elm.checked;
    }
    It'll check all elements whose name begins with name. So you can do blah1, blah2, blah3, and it'll check em all if you do checkAll(this,'blah')
    Last edited by Anarchos; Aug 4, 2001 at 16:10.
    ck :: bringing chris to the masses.


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
  •