HTML events are "things" that happen to HTML elements. When JavaScript is used in HTML pages, it can react to these events (e.g., a button being clicked, a page loading, or a mouse moving).
There are two primary ways to assign events using the basic DOM model:
The simplest way is adding an event attribute directly to the HTML tag.
<button onclick="displayDate()">Click Me</button>
A cleaner way is assigning the event to an element's property in your JavaScript file.
document.getElementById("myBtn").onclick = displayDate;
Here are some of the most frequently used events in web development:
onclick: The user clicks an element.onchange: An HTML element has been changed (often used in
inputs/dropdowns).onmouseover / onmouseout: The user moves the
mouse over / away from an element.onkeydown: The user pushes a keyboard key.onload: The browser has finished loading the page.The onload and onunload events are triggered when the user enters
or leaves the page. They are often used to initialize variables or handle cookies.
<body onload="checkCookies()">
These events can be used to create interactive hover effects without using CSS :hover,
allowing for more complex logic.
<div onmouseover="mOver(this)" onmouseout="mOut(this)">Hover Me</div>
onload = check; not onload = check();).
this keyword inside an event handler to refer to
the current elementonchange is vital for validating search boxes and form inputsonload is used to check the user's browser type or versionaddEventListener, the modern way to handle
events