Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Adding Event Listeners to HTML Elements | DOM Event Handling and Forms
HTML & JavaScript Interactivity for Beginners
course content

Contenido del Curso

HTML & JavaScript Interactivity for Beginners

HTML & JavaScript Interactivity for Beginners

1. DOM and HTML Manipulation
2. DOM Event Handling and Forms
3. JavaScript HTML5 APIs
4. Beginner Projects with HTML + JavaScript

Adding Event Listeners to HTML Elements

In the previous chapter, you saw how events trigger function calls through event attributes inside an HTML element. However, that method has many drawbacks. If an element had multiple events, putting all the event attributes to handle each of them inside an HTML tag makes the code look bloated and unreadable.

Therefore a better way around this issue is to keep the functionality separate from the structure. Thus it would help if you add an event listener to each HTML element through JavaScript, and then when the event triggers, the relevant function will be called.

Let's start with a simple example for the click event.

Event Listener with Click Event

html

index

css

index

js

index

copy

Adding Multiple Event Listeners

You can add multiple event listeners to the same HTML element and call different functions for each triggered event. This is ideal in scenarios where the HTML element handles numerous events.

html

index

css

index

js

index

copy

Passing Parameters to Event Listeners

You can also pass parameters to an event listener. In the below example, you'll discover how you can pass the element that triggered the event and get its value. Likewise, you can pass any numerical values and so on.

html

index

css

index

js

index

copy

Event Bubbling and Capturing

You can propagate events in the HTML DOM in two ways: bubbling and capturing.

Event propagation is the process of the occurrence of events from child elements to parent elements or vice versa.

Suppose there is a <div> element that houses a paragraph element, and if the event handler is placed on both elements, there will be a question of what happens when a user clicks on the <div> element, for instance.

  • Bubbling: The inner element handles the event first, followed by the outer element;
  • Capturing: The outer element handles the event first, followed by the inner element.

You can decide whether to use bubbling or capturing based on the parameters set on the useCapture parameter. By default, it is set to false, which is bubbling, and if set to True, it will use capturing.

html

index

css

index

js

index

copy
1. Which is the correct way to add an event listener to a button element with an id 'demo' and call the function myFunc on the onclick event?
2. What's the difference between bubbling and capturing?

Which is the correct way to add an event listener to a button element with an id 'demo' and call the function myFunc on the onclick event?

Selecciona la respuesta correcta

What's the difference between bubbling and capturing?

Selecciona la respuesta correcta

¿Todo estuvo claro?

Sección 2. Capítulo 2
We're sorry to hear that something went wrong. What happened?
some-alt