Check and Uncheck all checkboxes using jquery

Posted on Updated on

There are some simple things to do in programming yet some developers find it tough. This is an example of checking all the checkboxes in a page using jquery. For this to work , you need jquery.js file included in your page.

This is the body part with all the checkboxes:

<td align=”left” colspan=”4″><input type=”checkbox”  class=”checkAll”></td>
<td> <input type=”checkbox” name=”myChkBox”  class=”selectedId” id=”myChkBox1″ /> </td>
<td> <input type=”checkbox” name=”myChkBox”  class=”selectedId” id=”myChkBox2″ /> </td>

<td> <input type=”checkbox” name=”myChkBox”  class=”selectedId” id=”myChkBox3″ /> </td>

<td> <input type=”checkbox” name=”myChkBox”  class=”selectedId” id=”myChkBox4″ /> </td>


This is the jquery code:

$(function () {
$(‘.checkAll’).click(function () {
$(‘.selectedId’).attr(‘checked’, this.checked);

$(‘.selectedId’).change(function () {
var check = ($(‘.selectedId’).filter(“:checked”).length == $(‘.selectedId’).length);
$(‘.checkAll’).attr(“checked”, check);

Please note here i have used class attribute in both the checkboxes(One for checkall and the other for ChkBox1……ChkBox4).  The main check box for checking all the boxes has been given the class name as checkAll and all other sub checkboxes classname is given as selectedId.  Thats it. All your check box will be checked and unchecked.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s