- Home
- /
- Tutorials
- /
- JavaScript Tutorial
- /
- JavaScript Events
JavaScript Events
JavaScript Events
A JavaScript event is something that happens on a page, and code can run in response.
A click, a key press, a form being submitted - each of these is an event.
Events are what turn a page from something you read into something you use.
Example
Example
javascript
document.body.innerHTML = '<button id="btn">Click me</button>';
const btn = document.getElementById("btn");
btn.addEventListener("click", function () {
console.log("Button was clicked");
});
btn.click();