Rasika Rasika - 6 months ago 10
jQuery Question

Enable and disable check boxes using javascript

Table with two checkboxes

I have a data table and it includes two check boxes called Manufacturing Defects and Scrap Items.I need to uncheck Scrap Items,when I check the Manufacturing Defects vise versa.
Please help me to find the answer.
Bellow is my code part, I used to create checkboxes

row += '<td class="Item_Rejected1">' + '<input type="checkbox" name="active" value="checked" class="form-control input-sm" onchange="isActive()">' + '</td>';
row += '<td class="Item_Rejected2">' + '<input type="checkbox" name="active" value="checked" class="form-control input-sm" onchange="isActive()">' + '</td>';

Answer

Use .not(SELECTOR) to exclude current element

$('input[type="checkbox"]').change(function() {
  if (this.checked)
    $('input[type="checkbox"]').not(this).prop('checked', !this.checked);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<table>
  <tr>
    <td class="Item_Rejected1">
      IR1
      <input type="checkbox" name="active" value="checked" class="form-control input-sm">
    </td>
    <td class="Item_Rejected2">
      IR2
      <input type="checkbox" name="active" value="checked" class="form-control input-sm">
    </td>
  </tr>
</table>