0

When I click it for the first time, it click, but doesn't turn off...

I've tried this one, it checked and uncheck but only once.

This one doesn't work for me either.

$(document).ready(function() {
  $("tr").click(function() {
    var checkbox = $(this).find("input[type=checkbox]");

    if (!checkbox.prop("checked", "")) {
      checkbox.prop("checked", "false");
    } else {
      checkbox.prop("checked", "true");
    }

  });
});
td{
    background:red;
    padding:10px 40px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tr>
    <td>
      <input type="checkbox" />
    </td>
    <td>test</td>
  </tr>
</table>
mplungjan
  • 169,008
  • 28
  • 173
  • 236

1 Answers1

1

Here is a toggle

$(document).ready(function() {
  $("tr").on("click", function(e) {
    const $target = $(e.target)
    const $checkbox = $(this).find("input[type=checkbox]");
  // only run code when NOT clicking checkbox
    if (!$checkbox.is($target)) {
      let checked = $checkbox.is(":checked")
      $checkbox.prop("checked", !checked)
    }
  });
});
td {
  background: red;
  padding: 10px 40px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tr>
    <td>
      <input type="checkbox" />
    </td>
    <td>test</td>
  </tr>
</table>
mplungjan
  • 169,008
  • 28
  • 173
  • 236