We help IT Professionals succeed at work.
Get Started

How to use Javascript to check all checkboxes in HTML table

f_o_o_k_y
f_o_o_k_y asked
on
331 Views
Last Modified: 2012-08-14
Hello Experts,
I need your help on this problem.
I have many HTML tables on single web page (example of two is below).
<table id="t1" border="1">
      <tbody><tr>
        <th colspan="7">
         Table 1
        </th>
      </tr>
      <tr>
        <th>
          Number:
        </th>
        <th>
          <input id="c1" type="checkbox"> To check:  		 	 	 	 	
        </th>
      </tr>
  <tr>
    <td>
      1338
    </td>

    <td>
       <input name="wydaj[20]" value="1" type="checkbox">
       <input name="nr[20]" value="42875" type="hidden">
    </td>
    
  </tr><tr>
    <td>

      1345
    </td>
    <td>
       <input name="wydaj[21]" value="1" type="checkbox">
       <input name="nr[21]" value="42876" type="hidden">
    </td>
  
</tr>
</tbody>
</table>
    <br>
    <table id="t2" border="1">
      <tbody><tr>
        <th colspan="7">
          Table 2
        </th>
      </tr>

      <tr>
        <th>
          Number:
        </th>
        <th>
          <input id="c3" type="checkbox"> To check:  		 	 	 	 	
        </th>
    
      </tr>
  <tr>
    <td>
      1938
    </td>
    <td>
       <input name="wydaj[22]" value="1" type="checkbox">
       <input name="nr[22]" value="42862" type="hidden">

    </td>
  
  </tr><tr>
    <td>
      1939
    </td>

    <td>
       <input name="wydaj[23]" value="1" type="checkbox">
       <input name="nr[23]" value="42863" type="hidden">
    </td>
  
    
</tr>
</tbody></table>

Open in new window


I need to be able to check/uncheck all of the check-boxes in each of the tables after I click on check-box in the header.

How can I do this ? I'm a beginner in javascript.

Best Regards
FooKy
Comment
Watch Question
Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2020
Commented:
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE