Spot the bug - #35

Quick one: event handling bug here.

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.

1 Like

You’re calling saveForm() immediately and passing its return value to addEventListener, so the log happens on page load and the click handler is basically undefined. Pass the function reference instead: button. addEventListener('click', saveForm) (or () => saveForm() if you need to wrap it).

1 Like