JavaScript | addEventListener() with Examples

Prerequisite: JavaScript events
An event is an important part of JavaScript.A web page respond according to an event occurred. Some events are user generated and some are generated by API’s. An event listener is a procedure in JavaScript that waits for an event to occur. The simple example of an event is a user clicking the mouse or pressing a key on the keyboard.

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);

Parameters:



  • event : event can be any valid JavaScript event.Events are used without “on” prefix like use “click” instead of “onclick” or “mousedown” instead of “onmousedown”.
  • listener(handler function) : It can be a JavaScript function which respond to the event occur.

JavaScript Code to show the working of addEventListener() method :
code #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<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>  

chevron_right


Output:
Before clicking the “Click here” button:

After clicking the “Click here” button:

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<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>

chevron_right


Output:
Before clicking the “Click here” button and hovering over text:

After clicking the “Click here” button and hovering over text:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.