How to Run Code When a Button Is Clicked with addEventListener in JavaScript
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
<element>.addEventListener(<event name>, <function>);
| Part | What 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:

<!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 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
| Mistake | What happens | Do 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 1 | Pass the name only: addLike |
Writing 'onclick' as the event name | Nothing happens on a click: the event is called click | Use '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 twice | It still runs once per click: a counter with the same listener added twice went up by 1 | Nothing to fix, but a different function added twice would run twice |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Clicks do nothing | The event name is wrong, or the function was called instead of passed | Check 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 ran | Check the id, and move the script below the element |
| The code runs on page load, not on click | Brackets after the function's name | Pass 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
- EventTarget: addEventListener() method, on MDN.
- Element: click event, on MDN.
- EventTarget: removeEventListener() method, on MDN.
