dimanche 19 mai 2019

Event listener for change in checkboxes checked by ulterior means

      
var checkboxes = document.querySelectorAll('input[type=checkbox]');
        
        
function func() {
    checkboxes[1].checked = true;
}
        
for(var i = 0; i < checkboxes.length; i++) {
     checkboxes[i].addEventListener("click", function(i) {
         console.log('cat')
  });
}
       
   
<input type="checkbox" id="Nutrition" name='health'/>
<input type="checkbox" id = "Activity" name='health'/>
<input type="checkbox" id = "Medical" name='health'/>
    
    
<button onclick = 'func()'>PRESS</button>
    
    

How do you get an event listener to respond when a checkbox is being checked by ulterior means, in my example by a button that calls a function that checks a checkbox. The event listener doesn't respond in this situation, only when I directly check a checkbox. Thanks.




Aucun commentaire:

Enregistrer un commentaire