Question

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>

Was it helpful?

Solution

You can use:

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

Fiddle Demo

OTHER TIPS

Try this:

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

  1. Have a change event on #select-all
  2. Find the input by name with attribute selector $('input[name="p_v01"]')
  3. every time when you check/uncheck it results in true/false.

Use below jQuery

<script>
 $(document).ready(function(){
   $('#select-all').click(function(){
      $('input[name="p_v01"]').attr("checked",$(this).is(':checked'));
   });

});
</script>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top