What is the best way to add an event in JavaScript ?

Last Updated : 22 Oct, 2026

JavaScript events make web pages interactive by allowing developers to respond to user actions, such as clicking a button, typing in a field, or moving the mouse. Event handlers are functions that run when these events occur on elements in the Document Object Model (DOM).

There are three common ways to register event handlers in JavaScript:

  • HTML event handler attributes: Add event handlers directly to HTML elements.
  • HTML DOM event handler properties: Assign a function to a property such as onclick.
  • HTML DOM addEventListener() method: Register one or more event listeners on an element.

1. Using HTML Event Handler Attributes

HTML event handler attributes allow you to specify JavaScript code directly in an HTML element. For example, the onclick attribute runs JavaScript when the user clicks an element.

Syntax:

<element onclick="script">

Example:

In this example, clicking the button calls the Geeks() function and displays an alert message.

HTML
<!--Driver Code Starts-->
<!DOCTYPE html>
<html>
<head>
    <title>HTML Event Handler</title>
</head>
<!--Driver Code Ends-->

<body>
    <button type="button" onclick="Geeks()">
        Click Me
    </button>

    <script>
        function Geeks() {
            alert("Hi there!");
        }
    </script>
</body>

<!--Driver Code Starts-->
</html>
<!--Driver Code Ends-->

Output: A button labeled Click Me appears on the page. When you click it, an alert displays the message Hi there!.

2. Using HTML DOM Event Handler Properties

DOM event handler properties allow you to assign a JavaScript function to an event property of an HTML element. For example, the onclick property handles click events.

Syntax:

element.onclick = function() {
// JavaScript code
};

Example:

In this example, the getElementById() method selects the button, and the onclick property assigns a function that displays an alert when the button is clicked.

HTML
<!--Driver Code Starts-->
<!DOCTYPE html>
<html>
<head>
    <title>DOM Event Handler Property</title>
</head>
<!--Driver Code Ends-->

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

    <script>
        const btn = document.getElementById("button");

        btn.onclick = function() {
            alert("Hi there!");
        };
    </script>
</body>

<!--Driver Code Starts-->
</html>
<!--Driver Code Ends-->

Output: A button labeled Click Me appears on the page. Clicking it displays the alert message Hi there!.

3. Using the addEventListener() Method

The addEventListener() method registers a function to run when a specified event occurs on an element. Unlike event handler properties, it allows multiple different listeners to be registered for the same event without overwriting one another.

Syntax:

element.addEventListener(event, listener);

Parameters:

  • event: A case-sensitive string specifying the event type, such as "click" or "mouseover".
  • listener: The function or event listener object that runs when the specified event occurs.

The method also supports an optional third argument for configuring event handling.

Example:

In this example, two separate click event listeners are registered on the same button. Both listeners run when the button is clicked.

HTML
<!--Driver Code Starts-->
<!DOCTYPE html>
<html>
<head>
    <title>addEventListener Example</title>
</head>
<!--Driver Code Ends-->

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

    <script>
        const btn = document.getElementById("button");

        btn.addEventListener("click", function() {
            alert("Hi there!");
        });

        btn.addEventListener("click", function() {
            alert("Welcome to GeeksforGeeks!");
        });
    </script>
</body>

<!--Driver Code Starts-->
</html>
<!--Driver Code Ends-->

Output: A button labeled Click Me appears on the page. When you click it, two alert messages appear in sequence:

  1. Hi there!
  2. Welcome to GeeksforGeeks!

The second alert appears after the first alert is dismissed.

Comment