Interactive web pages respond to user actions without loading a new document. A JavaScript event listener connects an action, such as clicking a button, to a function that changes the page.

addEventListener() registers a callback on a target such as a button, document, or window. The event name is case-sensitive, so a button click uses “click” rather than “Click” or “onclick”.

A visible click counter makes the response easy to see without opening the developer console. The page and its script sit in the same local website folder, with defer allowing the browser to parse the button before the script selects it.

Steps to add a JavaScript event listener:

  1. Create index.html in your local website folder using your text editor.
    index.html
    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <title>Button clicks</title>
      <style>
        body { margin: 2rem; font: 1.1rem/1.5 system-ui, sans-serif; color: #172033; }
        main { max-width: 28rem; padding: 1.5rem; border: 1px solid #cbd5e1; border-radius: 0.5rem; }
        h1 { margin-top: 0; }
        button { padding: 0.6rem 1rem; font: inherit; cursor: pointer; }
        #click-status { font-weight: bold; }
      </style>
      <script src="clicks.js" defer></script>
    </head>
    <body>
      <main>
        <h1>Button clicks</h1>
        <button id="count-button" type="button">Count click</button>
        <p id="click-status" role="status">Clicks: 0</p>
      </main>
    </body>
    </html>

    The button's type=“button” prevents form submission if it is later placed inside a form. The status paragraph holds the text the listener will update.

  2. Create clicks.js beside index.html with the element references and counter.
    clicks.js
    const button = document.querySelector("#count-button");
    const status = document.querySelector("#click-status");
    let clickCount = 0;

    The selectors match the IDs in the HTML. The deferred script runs after those elements have been parsed.

  3. Append the handler function after the counter declaration in clicks.js.
    clicks.js
    function countClick() {
      clickCount += 1;
      status.textContent = `Clicks: ${clickCount}`;
    }

    Each call increments the counter and replaces the status text with its new value.

  4. Append the listener registration after the function in clicks.js.
    clicks.js
    button.addEventListener("click", countClick);

    countClick passes the function itself to the browser. countClick() would call it immediately instead of registering it for a later click.

  5. Save both files in your text editor.
  6. Open the saved index.html file in your browser.
  7. Click Count click once.
  8. Click Count click a second time.

    After the first click, the status reads Clicks: 1.

  9. Confirm that the status now reads Clicks: 2.

    The change from 0 to 1 to 2 shows that the listener runs once for each click. Reloading the page starts the counter at 0 again.

Share this guide