Show/hide elements with checkbox check/unchek

Viewed 46

I am trying to show and hide tr of table with checkbox check and uncheck. I tried the following script not working for me.

$('.checks').on('ifChecked', function(event) {
  var checked = $(this).val();
  if (checked == 1) {
    $('.vegetables').show();
  }
  if (checked == 2) {
    $('.fruits').show();
  }
});
.hidden {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-group">
  <label class="checkbox-inline">
   <input type="checkbox" class="checks" value="1">vegetables
   </label>
  <label class="checkbox-inline">
   <input type="checkbox" class="checks" value="2">Fruits
   </label>
</div>

<table>
  <tbody>
    <tr class="vegetables hidden">
      <td colspan="2">
        <h2>Vegetablese:</h2>
      </td>
    </tr>
    <tr class="vegetables hidden">
      <td>
        <label>Vegetables:</label>
        <input type="text">
      </td>
    </tr>
    <tr class="fruits hidden">
      <td colspan="2">
        <h2>Fruits:</h2>
      </td>
    </tr>
    <tr class="fruits hidden">
      <td>
        <label>Fuits:</label>
        <input type="text">
      </td>
    </tr>
  </tbody>
</table>

2 Answers

Here's what you need to do:

  • Change ifChecked with onchange. There's no event like ifChecked.
  • Use toggle based on the state of the checkbox.
  • Check for $(this).checked to find the state. This will return true or false.

$('.checks').on('change', function(event) {
  var checked = $(this).val();
  if (checked == 1) {
    $('.vegetables').toggle($(this).checked);
  }
  if (checked == 2) {
    $('.fruits').toggle($(this).checked);
  }
});
.hidden {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-group">
  <label class="checkbox-inline">
    <input type="checkbox" class="checks" value="1" /> vegetables
  </label>
  <label class="checkbox-inline">
    <input type="checkbox" class="checks" value="2" /> Fruits
  </label>
</div>

<table>
  <tbody>
    <tr class="vegetables hidden">
      <td colspan="2">
        <h2>Vegetablese:</h2>
      </td>
    </tr>
    <tr class="vegetables hidden">
      <td>
        <label>Vegetables:</label>
        <input type="text">
      </td>
    </tr>
    <tr class="fruits hidden">
      <td colspan="2">
        <h2>Fruits:</h2>
      </td>
    </tr>
    <tr class="fruits hidden">
      <td>
        <label>Fuits:</label>
        <input type="text">
      </td>
    </tr>
  </tbody>
</table>

Theres a couple of issues in your logic. Firstly ifChecked is not a valid event. Use change instead. Secondly the logic doesn't work to hide elements. To do that you'd need to loop through all checkboxes and determine their state in order to show/hide the content underneath.

The simplest and most extensible way to achieve what you require is to use a data attribute to specify which element should be toggled when the checkbox is updated. Then you can simply hide/show each element in the set. Try this:

let $checks = $('.checks').on('change', () => $checks.each((i, el) => $(el.dataset.target).toggle(el.checked)));
.hidden {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-group">
  <label class="checkbox-inline">
   <input type="checkbox" class="checks" value="1" data-target=".vegetables">vegetables
   </label>
  <label class="checkbox-inline">
   <input type="checkbox" class="checks" value="2" data-target=".fruits">Fruits
   </label>
</div>

<table>
  <tbody>
    <tr class="vegetables hidden">
      <td colspan="2">
        <h2>Vegetablese:</h2>
      </td>
    </tr>
    <tr class="vegetables hidden">
      <td>
        <label>Vegetables:</label>
        <input type="text">
      </td>
    </tr>
    <tr class="fruits hidden">
      <td colspan="2">
        <h2>Fruits:</h2>
      </td>
    </tr>
    <tr class="fruits hidden">
      <td>
        <label>Fuits:</label>
        <input type="text">
      </td>
    </tr>
  </tbody>
</table>

Related