Events
addEventListener runs a function when something happens, such as a click, key press or form submit.
Example
const box = document.getElementById("demo");
const button = document.createElement("button");
button.textContent = "Click me";
let count = 0;
button.addEventListener("click", () => {
count++;
box.textContent = "Clicked " + count + " times";
});
document.body.appendChild(button);Exercise
Run the function on click.
button.addEventListener("", () => { alert("Hi"); });
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.