js addeventlistener w code example
Example 1: javascript onclick event listener
document.getElementById("myBtn").addEventListener("click", function() {
alert("Hello World!");
});
Example 2: addeventlistener js to change style
var button = document.querySelector("button");
button.addEventListener("click", function() {
const curColour = document.body.style.backgroundColor;
if (curColour === 'red') {
document.body.style.backgroundColor = "blue";
}
else {
document.body.style.backgroundColor = "red";
}
});