Understanding DOM Events
Namima Islam
Posted on September 30, 2024
Ever use a webpage and wonder, "WOAH!! how was I able to click on that image, or how was I able to like that pic!"
Well, I'm here to tell you about this cool thing called DOM Events!
DOM Events is a great way to make a webpage interactive. Some examples of how DOM events make a page interactive is by typing on a page, clicking, tapping, submitting forms and many more!
In this article we will simplify DOM Events and learn how we can use them to create dynamic webpages.
What are DOM Events?
First, let's start by defining what D.O.M. stands for.
DOM stands for - Document Object Model. DOM is a programming interface that allows us to create, change, or remove elements from the document.
An event in Javascript, has the ability to "listen" for what is happening inside the browser.
In simple terms, DOM Events are actions or occurrences that happen in a web page, which JavaScript can detect and respond to. The events allows your web page to be interactive by triggering specific functions when a user interacts with elements on the page.
Listening to an event using addEventListener
For JavaScript to handle an event, we need to tell it to listen for that event. To do this we start by calling the method, addEventListener. We then need to pass in two arguments.
- The name of the event to listen for.
- A callback function to "handle" the event.
Example:
`img.addEventListener('click', (e) => handleClick(cocktail));`
In the code above, the img references the element, the addEventListener is attaching an event to the element, the click is the type of event it is listening for, the (e) is the event object, followed by the arrow function which is necessary to pass the specific argument to the handle click function(if not used handle click would be called immediately), then we have the handleClick which is the callback function, and once the cocktail is passed to the handleClick the data displays.
The click event is triggered when a user clicks on an element. This event is commonly used on buttons, links, and images. The click event is also a mouse event(will be covered later in the blog).
addEventListener detects and responds to specific events on a webpage, we saw an example using click but what about other events?
DOMContentLoaded is another DOM Event that can be ran with JavaScript Code.
Using DOMContentLoaded
The DOMContentLoaded event ensures your JavaScript runs only after the DOM is fully loaded, preventing errors from trying to access elements before they're available.
Example:
document.addEventListener('DOMContentLoaded', main);
DOMContentLoaded, is an event that fires when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, or other external resources to fully load.
It ensures that the DOM is fully built and accessible, so you can safely interact with elements in the HTML, such as adding event listeners or manipulating the DOM.
In the code above, the browser is telling you, that the document is now available and the user can start interacting with it.
Another common DOM Event are Mouse Events.
Mouse Events
We previously named one mouse event(click), here are some other common mouse events:
click - Triggered when the user clicks a mouse button over an element. Used for buttons, links, and any interactive elements where a user action is required.
dblclick - Triggered when the user double-clicks on an element. Used for actions that require confirmation or selection, such as editing text or opening files.
mousedown - Triggered when the mouse button is pressed down on an element. Used to initiate drag-and-drop operations or to create custom interactions that respond to mouse button presses.
mouseup - Triggered when the mouse button is released over an element. Often used with mousedown to complete an action, like dropping an item after dragging.
mousemove - Triggered when the mouse pointer moves within the bounds of an element. Used for interactive graphics, creating drawing applications, or for displaying dynamic information based on mouse position.
Mouse events are crucial for creating interactive user interfaces, they create instant reactions to a page, they make using a webpage user friendly and are engaging.
Our list of DOM Events doesn't stop there! There are many more events that can make a webpage interactive, if you want to read more on DOM Event types you can visit any of these sources:
Summary:
DOM Events are actions or occurrences that happen in a web page, which JavaScript can detect and respond too. The events allows your web page to be interactive by triggering specific functions when a user interacts with elements on the page, including keyboard actions and form submissions.
addEventListener detects and responds to specific events on a webpage.
Mouse events create instant reactions to a page.
DOMContentLoaded, ensures that the DOM is ready for JavaScript code to safely manipulate elements, add event listeners, or modify the content of the page. It's commonly used to run scripts after the page's structure is available and to ensure no errors occur due to trying to access elements that haven't yet been created.
Congratulations! You are now an expert in DOM Events!
Sources:
Cover Photo: https://www.pexels.com/search/web%20development/
https://learning.flatironschool.com/courses/8101/pages/javascript-events
https://www.w3schools.com/js/js_events.asp
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events
Posted on September 30, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 22, 2024