Answers for "event on checkbox"

12

javascript event listener checkbox

//js
var checkbox = document.querySelector("input[name=checkbox]");

checkbox.addEventListener( 'change', function() {
    if(this.checked) {
        // Checkbox is checked..
    } else {
        // Checkbox is not checked..
    }
});

//jquery
$('input[name=checkbox]').change(function(){
    if($(this).is(':checked')) {
        // Checkbox is checked..
    } else {
        // Checkbox is not checked..
    }
});
Posted by: Guest on July-08-2020
3

javascript checkbox onchange

<!-- Find values of checked boxes with jquery -->
<div class="fruit">
  <label>
    <span>Apple</span>
    <input onchange="checkValues()" name="apple" type="checkbox" value="apple">
  </label>

  <label>
    <span>Banana</span>
    <input onchange="checkValues()" name="banana" type="checkbox" value="banana">
  </label>

  <label>
    <span>Grape</span>
    <input onchange="checkValues()" name="grape" type="checkbox" value="grape">
  </label>
</div>

<script>
  function checkValues() {
    $('.fruit input:checkbox').each(function(index, element) {
      if (element.checked === true) {
        console.log(element.value)
      }
    });
  }
</script>
Posted by: Guest on July-28-2021

Code answers related to "event on checkbox"

Code answers related to "Javascript"

Browse Popular Code Answers by Language