Article image Events and listeners in Javascript

24. Events and listeners in Javascript

Page 74 | Listen in audio

Chapter 24: Events and Listeners in Javascript

Events in Javascript are actions or occurrences that happen in the system you are programming, be it a mouse click, a page loading or a key press. Events are one of the main mechanisms that allow interaction between the user and the interface.

In terms of web development, events are triggered in the browser, such as a mouse click, a page loading, a key press, etc. These events can be captured and handled using Javascript, allowing the creation of interactive and dynamic user interfaces.

Event Listeners

Event Listeners, or event listeners, are functions that 'listen' for some specific type of event to happen. When this event occurs, the Event Listener is triggered and performs an action.

To add an Event Listener to an HTML element, we use the 'addEventListener' method. This method receives two parameters: the name of the event you want to listen to and the function that will be executed when the event occurs.


element.addEventListener('click', function() {
    // Code to be executed when the event occurs
});

Event Listeners are a fundamental part of Javascript programming for the web, as they allow user interaction with the page. Without them, the page would be completely static and would have no way of reacting to user actions.

Types of Events

There are several types of events that can be listened to in Javascript. Some of the most common include:

  • click: triggered when the user clicks on an element;
  • dblclick: triggered when the user double-clicks on an element;
  • mouseover: triggered when the mouse cursor passes over an element;
  • mouseout: triggered when the mouse cursor leaves an element;
  • keydown: triggered when the user presses a key;
  • load: triggered when the page finishes loading.

Handling Events

When handling events, it is common to want to access the element that triggered the event. This can be done through the 'event' object which is automatically passed to the function being executed by the Event Listener. This object contains various information about the event, including the element that triggered it.


element.addEventListener('click', function(event) {
    console.log('The element that was clicked is: ', event.target);
});

In addition, it is also possible to prevent the default behavior of an event from happening. This is done through the 'preventDefault' method of the 'event' object.


element.addEventListener('click', function(event) {
    event.preventDefault();
    console.log('The click will not have the default effect.');
});

Events and Listeners in Javascript are a fundamental part of web development. They enable the creation of interactive and dynamic user interfaces, responding to user actions in real time. Therefore, it is essential that every Front End developer has a good understanding of them.

Now answer the exercise about the content:

What are Event Listeners in Javascript and how are they used?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Forms and Data Validation with Javascript

Next page of the Free Ebook:

75Forms and Data Validation with Javascript

4 minutes

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text