Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
HTML DOM click() Method
Next article icon

HTML DOM addEventListener() Method

Last Updated : 13 Jun, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

The addEventListener() method attaches an event handler to the specified element.

Syntax:

element.addEventListener(event, function, useCapture)

Note: The third parameter use capture is usually set to false as it is not used.

Below program illustrates the DOM addEventListener():

Example:




<!DOCTYPE html>
<html>
  
<head>
    <title>DOM Location host Property</title>
    <style>
        h1 {
            color: green;
        }
          
        h2 {
            font-family: Impact;
        }
          
        body {
            text-align: center;
        }
    </style>
</head>
  
<body>
    <h1>GeeksforGeeks</h1>
    <h2>addEventListener() method</h2>
  
    <p>
     This example uses the addEventListener() 
     method to add many events on the same 
     button.
    </p>
  
    <button id="myBtn">Try it</button>
  
    <p id="demo"></p>
  
    <script>
        var x = document.getElementById("myBtn");
        x.addEventListener("mouseover", myFunction);
        x.addEventListener("click", mySecondFunction);
        x.addEventListener("mouseout", myThirdFunction);
  
        function myFunction() {
            document.getElementById("demo").innerHTML += "Moused over!<br>"
            this.style.backgroundColor = "red"
        }
  
        function mySecondFunction() {
            document.getElementById("demo").innerHTML += "Clicked!<br>"
        }
  
        function myThirdFunction() {
            document.getElementById("demo").innerHTML += "Moused out!<br>"
        }
    </script>
  
</body>
  
</html>                    
 
 

Output:

  • Initially:
  • Mouse Over Event:
  • Mouse Clicked Event:
  • Mouse Out Event:

Supported Browsers: The browser supported by Location host Property are listed below:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari


Next Article
HTML DOM click() Method

M

MayankVandra
Improve
Article Tags :
  • JavaScript

Similar Reads

  • HTML DOM customElements define() Method
    The customElements define() method is used to define a new custom element. There are two types of custom elements that can be created: Autonomous custom element: These elements do not inherit from built-in HTML elements.Customized built-in element: These elements inherit from built-in HTML elements.
    2 min read
  • HTML DOM createDocument() Method
    The DOMImplementation createDocument() method is used to create and return a Document. Syntax: var doc = document.implementation.createDocument(namespaceURI, qualifiedNameStr, docType); parameters: namespaceURI: It is a DOMString containing the namespace URI of the document to be created, or null if
    1 min read
  • HTML DOM click() Method
    The click() method is used to simulate the mouse click on an element. This method works exactly the same as the element is manually clicked. Syntax: HTMLElementObject.click() Parameters: No parameters required. Return Value: No return value. Example: In this example, when the cursor goes over the ra
    1 min read
  • HTML DOM createHTMLDocument() Method
    The DOMImplementation createHTMLDocument() method is used to create a new HTML Document. Syntax: newDoc = document.implementation.createHTMLDocument(title); Parameters: title (Optional): It is a DOMString containing the title to be used for the new HTML document. Return Value: This function returns
    1 min read
  • HTML DOM before() method
    The before() method is used to insert a set of Node objects or HTML DOMString objects in the children list of the ChildNode's parent. The element is inserted before the ChildNode that we have mentioned. Syntax: ChildNode.before(Node or DOMString) Parameter: This method accepts a single parameter as
    2 min read
  • HTML DOM customElements get() method
    The customElements get() method returns the constructor used for a previously-defined custom element. Syntax: constructor = customElements.get('custom-element-name'); Parameters: name: The name of the custom element whose constructor you want to return. Return value: This method returns the construc
    1 min read
  • JavaScript addEventListener() with Examples
    The addEventListener() method is used to attach an event handler to a HTML document. 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" in
    4 min read
  • HTML DOM getElementsByName() Method
    The getElementsByName() method returns collection of all elements of particular document by name. This collection is called node list and each element of the node list can be visited with the help of the index. Syntax: document.getElementsByName(name) Parameter:This function accepts name of document
    2 min read
  • HTML DOM getElementById() Method
    The getElementById() method returns the elements that have given an ID which is passed to the function. This function is a widely used HTML DOM method in web designing to change the value of any particular element or get a particular element. If the passed ID to the function does not exist then it r
    3 min read
  • How addEventListener works for keydown on HTML 5 Canvas ?
    In HTML5, addEventListener can be used to capture keydown events on the entire document or a specific element like a canvas. Although the canvas doesn't directly receive keyboard input, you can attach the event listener to the document to detect key presses and interact with the canvas. ApproachCanv
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences