andybeak
7/6/2016 - 1:26 PM

jQuery to implement an "All of the above" checkbox

jQuery to implement an "All of the above" checkbox

<form action="demo_form.asp" method="get">
  <input type="checkbox" name="fruit" value="tomato"> I like Tomatoes
  <br>
  <input type="checkbox" name="fruit" value="strawberry"> I like Strawberries
  <br>
  <span class="checkbox-all"><input type="checkbox" name="all" value="all">I like all fruit</span>
  <br>
  <input type="submit" value="Submit">
</form>
$(".checkbox-all > input").change(function() {
  $(':checkbox').prop('checked', this.checked);
});

$("form > input").change(function() {
  if (this.checked == false) {
    $(".checkbox-all > input").prop("checked", false);
  }
})