https://www.w3schools.com/code/tryit.asp?filename=GKTIU8SG2RMP
document.addEventListener('DOMContentLoaded', function () {
var checkbox = document.querySelector('input[type="checkbox"]');
checkbox.addEventListener('change', function () {
if (checkbox.checked) {
// do this
console.log('Checked');
} else {
// do that
console.log('Not checked');
}
});
});
Above the link, when the first button is clicked, I got its status perfectly (either checked or not checked). However, for the second button, it does not show the status (either checked or not checked). How do I solve this problem? Thank you very much for your attention. NOTE: I am using for loop for the button so there will be many buttons and it is inefficient if the solution were to declare the buttons one by one.