Skip to main content

How to Run Code When a Button Is Clicked with addEventListener in JavaScript

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

button.addEventListener('click', addLike) tells the browser to run the function addLike every time the button is clicked. Pass the function by its name, without brackets, and attach the listener once: it keeps working for every click after that.

Syntax​

Syntax
<element>.addEventListener(<event name>, <function>);
PartWhat it does
<element>The element to listen to, such as a button.
<event name>Which event to react to, as a string: 'click' for a click. Other events include 'submit' for a form and 'input' for typing.
<function>The function to run when the event happens, written without brackets. The browser calls it for you.

MDN's EventTarget: addEventListener() method page lists everything it accepts.

The Example​

The example page has a Like button and a count of likes that starts at 0. Each click of the button adds one to the count.

Step 1: Write the Code​

Create a file named index.html in a text editor, such as Visual Studio Code:

index.html, a page with a Like button and a paragraph Likes with a strong element 0 with the id count, and a script with an addLike function that adds one to the count's text, connected to the button with addEventListener click.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Like button</title>
</head>
<body>
<button type="button" id="like">Like</button>
<p>Likes: <strong id="count">0</strong></p>

<script>
const count = document.getElementById('count');

function addLike() {
count.textContent = Number(count.textContent) + 1;
}

document.getElementById('like').addEventListener('click', addLike);
</script>
</body>
</html>

addLike reads the number on the page with Number(count.textContent), adds one, and writes the result back.

Step 2: Click the Button Three Times​

Double-click index.html to open it in your browser, then click Like three times:

The page in Chromium after three clicks: the Like button, and below it Likes: 3.

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.

What Happened​

  • The script ran once, when the page loaded, and attached the listener to the button.
  • Each click ran addLike, so the count went from 0 to 3.
  • The listener stayed attached, so you didn't need to do anything between clicks.

Common Mistakes​

MistakeWhat happensDo this instead
Adding brackets, addEventListener('click', addLike())addLike runs once while the page loads, and clicks do nothing: the count was 1 before any click and stayed 1Pass the name only: addLike
Writing 'onclick' as the event nameNothing happens on a click: the event is called clickUse 'click'
Running the script before the button exists, in <head>TypeError: Cannot read properties of null (reading 'addEventListener')Put the <script> at the end of <body>
Attaching the same function twiceIt still runs once per click: a counter with the same listener added twice went up by 1Nothing to fix, but a different function added twice would run twice

Troubleshooting​

SymptomLikely causeFix
Clicks do nothingThe event name is wrong, or the function was called instead of passedCheck for 'click' and remove the brackets after the function's name
Cannot read properties of null (reading 'addEventListener')The element wasn't found when the script ranCheck the id, and move the script below the element
The code runs on page load, not on clickBrackets after the function's namePass the function, not its result

FAQ​

How do I stop listening?​

Call removeEventListener with the same event name and the same function: after button.removeEventListener('click', addLike), clicks no longer run it.

Can the function know which element was clicked?​

Yes. The browser passes an event object to the function: function addLike(event) { ... }, and event.target is the clicked element.

What's the difference from the onclick attribute?​

<button onclick="addLike()"> puts code in the HTML and allows only one handler. addEventListener keeps the code in the script and lets you attach several functions to the same event.

Conclusion​

addEventListener('click', yourFunction) connects a button to the code it should run, once, for every click to come. Pass the function without brackets, spell the event 'click', and run the script after the button exists.

Additional Resources​