Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Apr 2013
    Posts
    135
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Unhappy one checkbox check all - not checking all

    Hi, I am trying to add a checkbox to my page that checks all other checkboxes, it's not really working as it should at the moment.

    here is my code:

    Code:
    <script language="JavaScript">
    function CheckAllmessages(source) {
      checkboxes = document.getElementsByName('allmessages');
      for(var i=0, n=checkboxes.length;i<n;i++) {
        checkboxes[i].checked = source.checked;
        alert('checked');
      }
    }
    </script>
    check all checkbox:
    Code:
    <td align="center"><strong>test<input name="allmessages" type="checkbox" id="allmessages" onclick="CheckAllmessages(this)" value="1" /> </strong></td>
    other checkboxes
    Code:
    <td align="center"><input type="checkbox" name="make_read[<?php echo $search['message_id']; ?>]" id="allmessages" value="1" /></td>
    Please help

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,907
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Try this:-

    Code:
    <form id="myform">
    <div id = "mydiv">
    <input type="checkbox" name="chk1">
    <input type="checkbox" name="chk2">
    <input type="checkbox" name="chk3">
    </div>
    <br>
    Check/Uncheck All: <input type="checkbox" name="checkall" onclick="checkedAll();">
    </form>
    
    <script type = "text/javascript">
    var checked = false;
    function checkedAll () {
    checked == true? checked = false:checked = true;
    var els = document.getElementById("mydiv").getElementsByTagName("input");
    var len = els.length;
    for (var i = 0; i < len; i++) {
    els[i].checked = checked;
    }
    }
    </script>

    "A man would do nothing, if he waited until he could do it so well that no one at all would find fault with what he has done." - Cardinal Newman

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •