Search code examples
jquerycheckbox

How to select/deselect all checkboxes in jQuery?


I am using the following sample HTML code below. With jQuery, I am unsure how to implement a "Select/Deselect All" using the id="select-all" to check / uncheck all the below checkboxes where name="p_v01"

<span style="font-size:8px;">Select All</span><br>&nbsp;
<input type="checkbox" name="select-all" id="select-all">
<span style="font-weight:bold;font-size:16px;padding-left:20px;">Emps</span>
<table summary="" role="presentation" class="checkbox_group">
  <tbody>
   <tr>
    <td>
     <input type="checkbox" id="P2_EMP_CB_0" name="p_v01" value="ANALYST">
     <label for="P2_EMP_CB_0">ANALYST</label>
    </td>
    <td>
     <input type="checkbox" id="P2_EMP_CB_1" name="p_v01" value="CLERK">
     <label for="P2_EMP_CB_1">CLERK</label>
    </td>
    <td>
     <input type="checkbox" id="P2_EMP_CB_2" name="p_v01" value="MANAGER">
     <label for="P2_EMP_CB_2">MANAGER</label>
    </td>
    <td>
     <input type="checkbox" id="P2_EMP_CB_3" name="p_v01" value="PRESIDENT">
     <label for="P2_EMP_CB_3">PRESIDENT</label>
   </td>
   <td>
    <input type="checkbox" id="P2_EMP_CB_4" name="p_v01" value="SALESMAN">
    <label for="P2_EMP_CB_4">SALESMAN</label>
  </td>
 </tr>
</tbody>


Solution

  • You can use:

    $('#select-all').click(function() {
        $('input[name="p_v01"]').prop('checked',this.checked);    
    });
    

    Fiddle Demo