Spot the bug - #40

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 nothing runs on click. Pass the function reference instead: button. addEventListener('click', saveForm) (or () => saveForm()).

You’re calling saveForm() immediately and passing its return value to addEventListener, so nothing runs on click. Pass the function reference instead: button. addEventListener('click', saveForm) (or () => saveForm() if you need to pass args).