You are currently viewing JavaScript Cheat Sheet 10.61: JavaScript HTML DOM EventListener

JavaScript Cheat Sheet 10.61: JavaScript HTML DOM EventListener

Introduction to JavaScript HTML DOM EventListeners

In this tutorial, we will focus on JavaScript HTML DOM Event Listeners. DOM (Document Object Model) represents the structure of the web page and allows JavaScript to interact with it. Event listeners are functions that wait for a specific event to occur and then execute some code in response.

1. Adding Event Listeners in JavaScript:
Event listeners can be added to HTML elements using JavaScript. The addEventListener() method is used to attach an event handler to the specified element.

Syntax:

element.addEventListener(event, function, useCapture);
  • event: Specifies the name of the event.
  • function: Specifies the function to run when the event occurs.
  • useCapture: (Optional) A Boolean value that specifies whether the event should be executed in the capturing or bubbling phase. Default is false.

Example:
Suppose we have a button in our HTML with the id myButton. We want to execute a function handleClick() when the button is clicked.

HTML:

<button id="myButton">Click Me</button>

JavaScript:

document.getElementById("myButton").addEventListener("click", handleClick);

function handleClick() {
    console.log("Button clicked!");
}

Explanation:

  • We use document.getElementById() to get the button element by its id.
  • We then call addEventListener() on the button element, passing "click" as the event and handleClick as the function to execute when the event occurs.
  • Inside the handleClick() function, we simply log a message to the console.

2. Event Types:
There are various types of events that can occur in an HTML document. Some common events include click, mouseover, keydown, submit, etc.

Example:
Let’s add event listeners for different types of events:

HTML:

<button id="myButton">Click Me</button>

JavaScript:

document.getElementById("myButton").addEventListener("click", handleClick);
document.getElementById("myButton").addEventListener("mouseover", handleMouseOver);
document.addEventListener("keydown", handleKeyDown);

function handleClick() {
    console.log("Button clicked!");
}

function handleMouseOver() {
    console.log("Mouse over the button!");
}

function handleKeyDown() {
    console.log("Key pressed!");
}

Explanation:

  • We add event listeners for three different events: click, mouseover, and keydown.
  • Each event is associated with a specific function that will be executed when the event occurs.

3. Event Listener Parameters:
The addEventListener() method can take an optional third parameter useCapture, which indicates whether the event should be executed in the capturing or bubbling phase.

Example:
Let’s add an event listener with and without using the capturing phase:

HTML:

<div id="outer">
    <div id="inner">Inner Div</div>
</div>

JavaScript:

document.getElementById("outer").addEventListener("click", handleOuterClick);
document.getElementById("inner").addEventListener("click", handleInnerClick);

function handleOuterClick() {
    console.log("Outer div clicked!");
}

function handleInnerClick() {
    console.log("Inner div clicked!");
}

Explanation:

  • In this example, we have two nested <div> elements, with the inner <div> inside the outer <div>.
  • When you click on the inner <div>, both the inner and outer event listeners are triggered.
  • By default, event listeners use the bubbling phase, where the event is first captured by the innermost element and then propagated to the outer elements.
  • You can use the third parameter useCapture to change this behavior. Set it to true to use the capturing phase.

4. Removing Event Listeners:
You can also remove event listeners using the removeEventListener() method.

Syntax:

element.removeEventListener(event, function);

Example:
Let’s add and then remove an event listener:

HTML:

<button id="myButton">Click Me</button>

JavaScript:

document.getElementById("myButton").addEventListener("click", handleClick);

function handleClick() {
    console.log("Button clicked!");
    // Remove the event listener after the first click
    document.getElementById("myButton").removeEventListener("click", handleClick);
}

Explanation:

  • In this example, we add a click event listener to the button that logs a message to the console when clicked.
  • Inside the handleClick() function, we also remove the event listener using removeEventListener() after the first click.
  • Therefore, the button will only respond to the click event once.

Conclusion


Understanding JavaScript HTML DOM Event Listeners is essential for creating interactive web applications. By following this tutorial, you should now have a good grasp of how to add, handle, and remove event listeners in JavaScript. Experiment with different events and scenarios to further solidify your understanding. Happy coding!

Leave a Reply