Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have done the coding to select multiple checkbox.i.e by clicking upon one checkbox ,all should be select.but it is not working .below is my coding.please help me.

<script>
 
	checked=false;
	function checkedAll() 
	{
		var aa= document.getElementById('frm1');
 
		if(checked == false)
		{
 
			checked = true;
		}
		else
		{  
			checked = false;
		}
	
		for (var i =0; i < aa.elements.length; i++) 
		{  
			aa.elements[i].checked = checked;
		 
		}
	
	 }
  
</script>

<aside  class="right-side"> <
  <section class="content">
    <div class="row">
      <div class="col-xs-12">
        <div class="box">
          <div class="box-header">
            <h3 class="box-title">Subscriber Listing</h3>
          </div>
          <div class="box-body table-responsive">
            
            <form action="<? echo $formsubmit; ?>" id="frm1" method="post" >
              <table id="example2" class="table table-bordered table-hover">
                <thead>
                  <tr>
                    <th width="10%" style="padding-left:5px;"><input type="checkbox" onClick="checkedAll();" name="checkall">
                      Select All</th>
                     
                  </tr>
                </thead>
                <tbody>
                  
                  <tr>
                    <td width=10%><input name="chk1[]" type="checkbox" value="<?php echo $cat['new_id'];?>" id="chk1[]" class=""/></td>
                     
                  </tr>
                 
                   
                </tbody>
              </table>
            </form>
          
          </div>
        </div>
      </div>
    </div>
  </section>
</aside>
Posted
Updated 30-Dec-14 2:05am
v2
Comments
Kornfeld Eliyahu Peter 30-Dec-14 8:23am    
You can not run on all elements in side the form - not all of them are checkboxes...
Assign the same name (but different id) to all checkboxes and use getElementsByName to get ans set them...

1 solution

Try this:
XML
<!DOCTYPE html>
<html>
<head>
<script>
function checkedAll(e) {
     var checkboxes = document.getElementsByTagName('input');

     for (var i = 0; i < checkboxes.length; i++) {
         if (checkboxes[i].type === 'checkbox') {
             checkboxes[i].checked = e.checked;
         }
    }

 }
</script>
</head>
<body>
<table>
<tr><input type="checkbox" onChange="checkedAll(this);" name="checkall">
Select All
</tr>

<tr>
<td width=10%><input name="chk1[]" type="checkbox" value="apple" >Apple</td>
</tr>

<tr>
<td width=10%><input name="chk1[]" type="checkbox" value="orange" >Orange</td>
</tr>

</table>

</body>
</html>
 
Share this answer
 
Comments
Sradhanjali Behera 6-Jan-15 5:46am    
thank you it helps me a lot @Peter Leow
Peter Leow 6-Jan-15 5:49am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900