0

I've a html checkbox which select all checkbox after click. But unfortunately it's not working. What's the problem in here ?

javascript Code:

<script language="JavaScript">
function toggle(source) {
 checkboxes = document.getElementsByName('contact_no');
  for(var i=0, n=checkboxes.length;i<n;i++) {
  checkboxes[i].checked = source.checked;
  }
}</script>

Php code:

echo "<table width='100%' cellpadding='0' cellspacing='0'>";
echo "<tr>";
echo "<td class='tdhead' valign='top' width='100'><b>Contact Name</b></td>";    
echo "<td class='tdhead' valign='top' width='100'><b>Contact 
Number</b>";                
echo '<input type="checkbox" onClick="toggle(this)" /> Toggle 
All<br/>';              
echo "</tr>";


while($row = mysqli_fetch_array($result))
{  
  $gid = $row['gid'];
  $contact_name = $row['contact_name'];  
  $contact_no =  $row['contact_no'];
  echo "<tr>";              
  echo "<td class='tdhead2' valign='top'>$contact_name</td>";
  echo "<td class='tdhead2' ><input type='checkbox' value='$gid' name='contact_no[]'' 
  />&nbsp;&nbsp;$contact_no</td>";                  
  echo "</tr>";
}

echo "</table>";
10
  • You should include your rendered html and not your php code as this question appears to be client side and unrelated to the php code. Also, what about this doesn't work? Do you get errors on the console, does it just do nothing? Commented Sep 26, 2013 at 3:47
  • u didnt use id for checkbox input type.. Commented Sep 26, 2013 at 3:48
  • possible duplicate of: stackoverflow.com/questions/386281/… Commented Sep 26, 2013 at 3:49
  • @Ashish which id i use ? Commented Sep 26, 2013 at 3:51
  • can you show us what's the content of that 'source' being passed in your function. Commented Sep 26, 2013 at 3:58

4 Answers 4

2

The name on each of your elements is: contact_no[] but in your JS code, you getElementsByName('contact_no') - add [] to the getElementsByName call.

Sign up to request clarification or add additional context in comments.

3 Comments

I use [] but it's not checked all checkboxes.
I'd also change your for loop to this: for(var i=0, i<checkboxes.length;i++) then as the answer below states, your tying to get the checked state of an event - not of the source (the argument passed to your event handler is an event)
If you're still stuck - update your question with the current code that isnt working.
1

for a safety, you should handle event object like code below

<script type="text/javascript">
function toggle(evt) {
 evt = window.event || evt;
 var source = evt.srcElement || evt.currentTarget 

 checkboxes = document.getElementsByName('contact_no');
  for(var i=0, n=checkboxes.length;i<n;i++) {
  checkboxes[i].checked = source.checked;
  }
}
</script>

3 Comments

Safety? How is that "safer"?
@RobG I think he is referring to the fact that evt may not be set in some browsers
The OP doesn't use an event object, and if it did, it could just pass it: toggle(event). I don't see how that's better (or safer) than passing this.
0
<script language="JavaScript">
    function toggle(source)
    {
        checkboxes = document.getElementsByName('contact_no');
        for(var i=0, n=checkboxes.length;i<n;i++)
        {
            contact_no[i].checked = source.checked;
        }
    }
</script>

Comments

0

One another solution, you can add Id attribute on the main check box from which you want to perform select all action and make the same as class attribute of other check boxes.

E.g.

$(document).ready(function(){
  $('.selectall').change(function({
    var id = $(this).attr('id');

    if ($(this)is(":checked")) {
      $("." + id).attr('checked','checked');

    } else {
      $("." + id).attr('checked','');
    }
  }))
})

I hope this code helps you

1 Comment

There is no jQuery tag. There is no need for an ID. That is very ordinary jQuery—it should use the prop method and set the value to a Boolean.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.