# Spot the bug - #100: Save Button Click

**URL:** <https://forum.kirupa.com/t/spot-the-bug-100-save-button-click/682792>\
**Category:** web dev\
**Created:** [July 28, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-100-save-button-click/682792 "2026-07-28T07:00:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![BobaMilk](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/bobamilk/32/31157_2.png) [@BobaMilk](https://forum.kirupa.com/u/BobaMilk)\
**Post date:** [July 28, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-100-save-button-click/682792/1 "2026-07-28T07:00:24Z")

</div>

Quick one: event handling bug here.

```js
const button = document.querySelector('#save');
button.addEventListener('click', saveForm());

function saveForm() {
  console.log('saved');
}

```

Reply with what is broken and how you would fix it.

---

<div class="post-metadata">

**Author:** ![Yoshiii](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/yoshiii/32/31156_2.png) [@Yoshiii](https://forum.kirupa.com/u/Yoshiii)\
**Post date:** [July 29, 2026, 8:00am UTC](https://forum.kirupa.com/t/spot-the-bug-100-save-button-click/682792/2 "2026-07-29T08:00:22Z")

</div>

Calling `saveForm()` right there runs it immediately and passes its return value (undefined) as the handler.

```auto
button.addEventListener('click', saveForm);

```

No parens, just pass the reference. Classic one, I still catch myself doing this occasionally when refactoring in a hurry.

---

<div class="post-metadata">

**Author:** ![BobaMilk](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/bobamilk/32/31157_2.png) [@BobaMilk](https://forum.kirupa.com/u/BobaMilk)\
**Post date:** [August 1, 2026, 5:00pm UTC](https://forum.kirupa.com/t/spot-the-bug-100-save-button-click/682792/3 "2026-08-01T17:00:09Z")

</div>

**Spot the Bug answer:** saveForm() is called immediately and its return value (undefined) is passed as the click handler, so saveForm never runs on click.

**The fix:**

```javascript
button.addEventListener('click', saveForm);

```

**Why:**  
Using saveForm() invokes the function right away during setup instead of passing a reference to it. addEventListener needs a function reference, not the result of calling the function, so the click event has no valid handler attached.
