# addEventListener runs my function immediately instead of on click

Source: https://coursesweb.net/forum/t/addeventlistener-runs-my-function-immediately-instead-of-on-click/
Site: CoursesWeb Forum, category JavaScript and the DOM
Published: 2026-12-02. Updated: 2026-12-03. Replies: 2.

## Question by Kaveh S., 2026-12-02

ok this is driving me crazy. my function runs the moment the page loads and then clicking the button does nothing at all. console.log shows "clicked" before I even touch anything.

```html
<button id="buy">Add to cart</button>
<script src="app.js"></script>
```

```js
const btn = document.querySelector("#buy");

function showMessage() {
  console.log("clicked");
}

btn.addEventListener("click", showMessage());
```

Console on page load:

```
clicked
```

and after that, nothing, however many times I click the button. No red errors either.

I added console.log everywhere to check. The function is found, the button is found. In my bootcamp the instructor wrote the same line with the same brackets, I think. So why does it fire early? I do not just want a fix, I would like to understand why a function that I "gave" to the button gets run by the page.

## Reply 1 by Mpho N., 2026-12-03

The browser needs to know it is a call, so put it in quotes: btn.addEventListener("click", "showMessage()"). That is how onclick="showMessage()" works in HTML, same idea. In HTML the browser reads the text and makes the function for you, so I think addEventListener should accept the text too.

## Reply 2 by Gaelle R., 2026-12-03

I think the brackets are the problem. showMessage() means "run it now". Try showMessage without brackets, so just the name of the function. I had the same problem in my first CSS animation project, but with JavaScript.

---
Cite as: CoursesWeb Forum, "addEventListener runs my function immediately instead of on click", https://coursesweb.net/forum/t/addeventlistener-runs-my-function-immediately-instead-of-on-click/, accessed 2026-12-03.
