2

Table with two checkboxes

I have a data table and it includes two check boxes called Manufacturing Defects and Scrap Items.I need to disable 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>';
abhilash-ram
  • 93
  • 12
Rasika
  • 43
  • 9

4 Answers4

1

(1) Detect click on any checkbox
(2) Uncheck ALL checkboxes
(3) Check the checkbox that was clicked

/*  javascript/jQuery  */
$('input[type=checkbox]').click(function(){
    $('input[type=checkbox]').prop('checked',false);
    $(this).prop('checked',true);
});
<!-- HTML -->
<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>
cssyphus
  • 37,875
  • 18
  • 96
  • 111
0

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>
Rayon
  • 36,219
  • 4
  • 49
  • 76
0

One more answer,

   

 $('input[type="checkbox"]').click(function() {
      if (this.checked)
        $('input[type="checkbox"]').attr('checked',false);
         $(this).prop('checked',true);
    });
<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>
Mahendra Kulkarni
  • 1,437
  • 2
  • 26
  • 35
-1

Here is the actual answer as requested by Rasika. Dont change the questions. It is the answer for that one. I got output .

[Here is the output] https://jsfiddle.net/Abhilash1994/btmfuwsg/

Manufacturer: <input type="checkbox" id="myCheck"> 
Scrap: <input type="checkbox" id="myCheck1">
    <script>
      $(document).ready(function() {
        $('#myCheck').change(function() {
            if ($(this).prop('checked')) {
                document.getElementById("myCheck1").disabled=true;
            }
            else {
               document.getElementById("myCheck1").disabled=false;
            }
        });
        $('#myCheck1').change(function() {
            if ($(this).prop('checked')) {
                document.getElementById("myCheck").disabled=true;
            }
            else {
               document.getElementById("myCheck").disabled=false;
            }
        });
    });
    </script>
abhilash-ram
  • 93
  • 12