Trigger an event when a checkbox is changed programmatically via JavaScript

前端 未结 2 715
礼貌的吻别
礼貌的吻别 2020-12-10 07:32

I need to know when a checkbox has been changed by Javascript using pure javascript.

I have setup an \'onchange\' event that successfully executes when the user chan

相关标签:
2条回答
  • 2020-12-10 07:59

    You might want to place it inside of a window.onload to make sure the dom is ready, $(document).ready equivalent without jQuery

    var checkbox = document.getElementById("test-check");
    checkbox.addEventListener("change", checked, false);
    
    function checked(e) {
      console.log(e.target.checked)
    }
    
    var event = new Event('change');
    checkbox.checked = !checkbox.checked;
    checkbox.dispatchEvent(event);
    <span>
      <input type="checkbox" id="test-check">
     Test
    </span>

    Also you have some alternatives to the change event

    Documentation

    0 讨论(0)
  • 2020-12-10 08:06

    You can create a change event with the document.createEvent/initEvent methods and then use the dispatchEvent method to dispatch the event from the specified checkbox element.

    var event = document.createEvent("HTMLEvents");
    event.initEvent('change', false, true);
    checkbox.dispatchEvent(event);
    

    This will allow you to programmatically change the checked property of the element and then trigger a change event that will thereby fire the event listeners.

    Here is a basic example:

    var checkbox = document.querySelector('[type="checkbox"]');
    var button = document.querySelector('button');
    
    // Event Listener:
    checkbox.addEventListener('change', function(event) {
      alert(event.target.checked);
    });
    
    // Programmatically change the `checked` property:'
    button.addEventListener('click', function() {
      checkbox.checked = !checkbox.checked;
      triggerEvent(checkbox, 'change');
    });
    
    function triggerEvent(element, eventName) {
      var event = document.createEvent("HTMLEvents");
      event.initEvent(eventName, false, true);
      element.dispatchEvent(event);
    }
    <input type="checkbox" />
    <button>Programmatically trigger 'change' event</button>

    As an alternative, you can also use the Event() constructor, however it has less browser support.

    var event = new Event('change');
    checkbox.dispatchEvent(event);
    

    Here is an example:

    var checkbox = document.querySelector('[type="checkbox"]');
    var button = document.querySelector('button');
    
    // Event Listener:
    checkbox.addEventListener('change', function(event) {
      alert(event.target.checked);
    });
    
    // Programmatically change the `checked` property:'
    button.addEventListener('click', function() {
      checkbox.checked = !checkbox.checked;
      triggerEvent(checkbox, 'change');
    });
    
    function triggerEvent (element, eventName) {
      var event = new Event(eventName);
      element.dispatchEvent(event);
    }
    <input type="checkbox" />
    <button>Programmatically trigger 'change' event</button>

    0 讨论(0)
提交回复
热议问题