In javascript what are the different ways of adding an event listener to browser DOM? i have tried the following code, and i was wondering if there are more ways to do this.
let div = document.getElementById("div");
let btn = document.getElementById("btn");
function sayHello() {
div.innerText = "Hello World";
console.log("awesome");
}
btn.addEventListener("click", sayHello);
In JavaScript, there are two ways for event handling.
addEventListener that you used yourself.onEvent, here is an example:Javascript
let hiButton = document.getElementById('someButton');
function sayHello() {
console.log("Hi");
}
hiButton.onclick = sayHello();