Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is event target in JavaScript

Understanding Event Target in JavaScript

The Basics of Event Target

Consider this, you're at a large party and someone pops a balloon. Everyone hears the sound, but not everyone knows where it came from. In this scenario, the popping balloon is akin to an event in JavaScript, and the event's target is the person who popped the balloon.

In the world of JavaScript, an event can be many things - a mouse click, a key press, or even a page load. The target of an event is the HTML element that triggered the event.

The Event Object and Its Target Property

Whenever an event is triggered in JavaScript, an event object is created. This object contains all kinds of useful information about the event, including the target property. The target property of the event object is a reference to the object that dispatched the event.

Here's an example of how you might see it used in JavaScript code:

element.addEventListener('click', function(event) {
  console.log(event.target);
});

In this example, we're adding an event listener to 'element'. When a click event is triggered, the function logs the event's target to the console.

Event Bubbling and Event Target

Event bubbling is another important concept to understand when learning about event targets. Going back to our party analogy, imagine now that the popping balloon was inside a box, which was inside another box, and so on. When the balloon pops, the sound bubbles up through each layer of box until it reaches the outermost one.

In JavaScript, when an event is fired on an element that has parent elements, the event is first captured and handled by the innermost element, and then propagated to outer elements.

However, no matter how many layers deep the event occurs, the event target always refers to the element where the event was initially dispatched. For example:

document.body.addEventListener('click', function(event) {
  console.log(event.target);
});

document.querySelector('button').click(); // logs <button></button>

In this example, even though the event listener is on the body element, the event target logged is the button element where the click event was dispatched.

Why is Event Target Useful?

The event target property can be incredibly useful because it allows us to access and manipulate the element that triggered the event. For example, we could change the color of a button when it's clicked:

element.addEventListener('click', function(event) {
  event.target.style.backgroundColor = 'red';
});

In this example, when the 'element' is clicked, the event target's (the clicked element) background color is changed to red. Without the event target property, we would have no way of directly accessing the clicked element within the event listener.

Conclusion

Understanding the event target in JavaScript is like learning to trace the source of a sound in a crowded room. It's a powerful tool that allows us to pinpoint and manipulate the origin of an event, adding new levels of interactivity and responsiveness to our websites. So next time when you're lost in a sea of events, remember the event target. It's your guiding star, leading you to the source of the action. Happy coding!