Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is target in JavaScript

Understanding Targets

If you've been dabbling in the world of JavaScript, you might have come across the term 'target'. But what exactly does it mean? In simple terms, a target in JavaScript is an object or element that triggers an event. Think of it like the 'who' in a sentence. If you click a button, the button is the target.

Targets and Events

In JavaScript, events are actions or occurrences that happen in the system you are programming, which the system tells you about so you can respond to them in some way if desired. For example, if the user clicks a button on a webpage, the 'click' is the event, and the button is the 'target' of that event.

Consider this piece of code:

document.querySelector("button").addEventListener("click", function(event) {
  alert("The button was clicked!");
});

In this example, the 'button' is the target. When the button is clicked, it triggers the 'click' event. The function then responds by displaying an alert that says "The button was clicked!"

Diving Deeper Into Event Targets

Event targets are part of the Event Object in JavaScript. The Event Object is a built-in object that contains properties and methods for any event. One of these properties is 'target'.

Let's use the following code as an example:

document.querySelector("button").addEventListener("click", function(event) {
  console.log(event.target);
});

Here, when the button is clicked, the function triggers and logs the event target to the console. If you were to click the button and then inspect the console in your browser developer tools, you would see something like <button></button>. This is the HTML representation of the button that was clicked - the event target.

Event Targets In Forms

Event targets can be incredibly useful when working with forms. Let's say we have a simple form on a webpage:

<form id="myForm">
  <input type="text" name="username" placeholder="Enter your username">
  <input type="submit" value="Submit">
</form>

And we have the following JavaScript code:

document.querySelector("#myForm").addEventListener("submit", function(event) {
  event.preventDefault();
  console.log(event.target.username.value);
});

In this example, when the form is submitted, the function triggers. The event.preventDefault() method stops the form from actually submitting (which would refresh the page). Then, we log the value of the 'username' input to the console. Here, the event target is the form, and we can access the values of the form elements directly from the target.

Analogy To Understand Event Targets

Think of event targets like the bullseye on a dartboard. When you throw a dart (event), it will land somewhere on the board (the target). The 'target' in JavaScript works in a similar way. When an event happens (like a click, or a form submission), it 'hits' a target. That target could be a button, an input field, a form, or any other element on the webpage.

Conclusion

To wrap it up, targets in JavaScript are the elements that trigger events. They are an integral part of how we interact with web pages and are a fundamental aspect of dynamic web development. Whenever an event occurs, remember that there's a target involved. It's like a dart hitting a dartboard, a key meeting a lock, or a button being pressed on a remote. The action (event) interacts with an object (target), and something happens in response. As you continue your journey in JavaScript, take note of the targets in your code and see how they shape your user interactions.