Open In App

JavaScript addEventListener() with Examples

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

The addEventListener() is an inbuilt function in JavaScript which takes the event to listen for, and a second argument to be called whenever the described event gets fired. Any number of event handlers can be added to a single element without overwriting existing event handlers. 

Syntax:

element.addEventListener(event, listener, useCapture);

Parameters:

  • event: event can be any valid JavaScript event. Events are used without “on” prefixes like using “click” instead of “onclick” or “mousedown” instead of “onmousedown”.
  • listener(handler function): It can be a JavaScript function that responds to the event occurring.
  • useCapture: It is an optional parameter used to control event propagation. A boolean value is passed where “true” denotes the capturing phase and “false” denotes the bubbling phase.

Example 1: In this example, we will display text on the webpage after clicking on the button.

HTML




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width,
                   initial-scale=1.0">
    <title>Document</title>
</head>
 
<body>
    <button id="try">Click here</button>
    <h1 id="text"></h1>
    <script>
        document.getElementById("try").addEventListener("click", function () {
            document.getElementById("text").innerText = "GeeksforGeeks";
        });
    </script>
</body>
 
</html>


Output: 

JavaScript addEventListener() with Examples

JavaScript addEventListener() with Examples

Example 2: In this example, two events “mouseover” and “mouseout” are added to the same element. If the text is hovered over then “mouseover” event occurs and the RespondMouseOver function is invoked, similarly for “mouseout” event RespondMouseOut function is invoked. 

HTML




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,
    initial-scale=1.0">
    <title>Document</title>
</head>
 
<body>
    <button id="clickIt">Click here</button>
 
    <p id="hoverPara">Hover over this Text !</p>
 
 
    <b id="effect"></b>
 
    <script>
        const x = document.getElementById("clickIt");
        const y = document.getElementById("hoverPara");
 
        x.addEventListener("click", RespondClick);
        y.addEventListener("mouseover", RespondMouseOver);
        y.addEventListener("mouseout", RespondMouseOut);
 
        function RespondMouseOver() {
            document.getElementById("effect").innerHTML +=
                "MouseOver Event" + "<br>";
        }
 
        function RespondMouseOut() {
            document.getElementById("effect").innerHTML +=
                "MouseOut Event" + "<br>";
        }
 
        function RespondClick() {
            document.getElementById("effect").innerHTML +=
                "Click Event" + "<br>";
        }
    </script>
</body>
 
</html>


Output: 

JavaScript addEventListener() with Examples

JavaScript addEventListener() with Examples

JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.



Last Updated : 18 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads