SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Mar 2011
    Location
    Manchester, UK
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript - Not Selecting All Checkboxes

    Hi Guys,

    I'm using Javascript to make all the checkboxes checked in a form, but when i click the button to check them all it will only select the first checkbox. Why is this?

    JAVASCRIPT:
    Code:
    	function checkUncheckAll(theElement) {
         var theForm = theElement.form, z = 0;
    	 for(z=0; z<theForm.length;z++){
          if(theForm[z].type == 'checkbox' && theForm[z].name != 'checkall'){
    	  theForm[z].checked = theElement.checked;
    	  }
         }
        }
    CODE:
    PHP Code:
    <input name="checkall" onclick="checkUncheckAll(this);" type="checkbox" />
      </tr>
    <tr class="tabletext">
    <?php
    $i
    =0;
    while (
    $i $num) {
    $LeadID=mysql_result($result,$i,"LeadID");
    $ProgramID=mysql_result($result,$i,"ProgramID");
    $ProgramName=mysql_result($result,$i,"ProgramName");
    $AffID=mysql_result($result,$i,"AffID");
    $AdID=mysql_result($result,$i,"AdID");
    $Status=mysql_result($result,$i,"Status");
    $Payout=mysql_result($result,$i,"Payout");
    ?>
    <tr>
    <td class="tableresultstxt"><? echo $LeadID?></td>
    <td class="tableresultstxt"><? echo $ProgramID?></td>
    <td class="tableresultstxt"><? echo $ProgramName?></td>
    <td class="tableresultstxt"><? echo $AffID?></td>
    <td class="tableresultstxt"><? echo $AdID?></td>
    <td class="tableresultstxt"><? echo $Status?></td>
    <td class="tableresultstxt"><? echo $Payout?></td>
    <td>
      <p>
        <input type="checkbox" name="theForm" id="<? echo $id?>" value="<? echo $id?>">
      </p>
    </form></td>
    </tr>
    <?
    $i
    ++;
    }
    mysql_close();
    ?>

    </table>
    Any help would be great please.

    Thanks.
    You're Help Does Not Go Unnoticed, I have So Far Donated 25 GBP
    To Cancer Research UK As A Thank You To All The SitePoint
    Members That Have Helped Me In The PHP Forum Thank You!

  2. #2
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    776
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Code JavaScript:
     var theForm = theElement.form
    This isn't referencing the form, or any elements inside of it.

    To reference all the checkboxes in this scenario, you could use document.getElementsByName():
    Code JavaScript:
     var theForm = document.getElementsByName("theForm")
    var details = {
    . . web: "afterlight.com.au",
    . . photos: "jvdl.id.au",
    . . psa: "usethelatestversion.com"
    }

  3. #3
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hopefully this function will help

    Code:
    <script type="text/javascript">
    
    function checkUncheckAll(obj) {
        var chkboxesO = document.getElementsByName('theForm'); //get checkboxes to check/uncheck
        for(i=0; i < chkboxesO.length; i++){
        	chkboxesO[i].checked = obj.checked;
        }
    }
    
    </script>

  4. #4
    SitePoint Addict
    Join Date
    Mar 2011
    Location
    Manchester, UK
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys, works now

    Thumbs Up!
    You're Help Does Not Go Unnoticed, I have So Far Donated 25 GBP
    To Cancer Research UK As A Thank You To All The SitePoint
    Members That Have Helped Me In The PHP Forum Thank You!


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
  •