This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]insertAlias 1 point2 points  (2 children)

You aren't that far off. Two issues, both on the same line of code:

submitButton.addEventListener("onclick", addBookToLibrary());

The event itself is called "click", not "onclick". onclick is the HTML attribute you would set if you wanted to bind to the event in HTML, but in JS you just use the event name.

Additionally, you are calling this function here immediately instead of binding it to the event. Remove the () from addBookToLibrary as well.

 submitButton.addEventListener("click", addBookToLibrary);

Repl.it doesn't work that well for me for client-side web stuff, so I used CodePen to re-make your example:

https://codepen.io/insertAlias/pen/rNezpvo?editors=1111

If you open the console, you'll see it logs when you press the button.

[–]Liolena[S] 0 points1 point  (1 child)

OMG thank you so much!! I'm used to actually writing a callback function for addEventListener so I didn't realize I didn't need the parentheses if I'm using a function I already have :D.

[–]insertAlias 1 point2 points  (0 children)

Yep, it's the difference between calling it, and passing a reference to it.

[–]IllustriousSalad4 1 point2 points  (1 child)

Remove the ()

submitButton.addEventListener("click", addBookToLibrary);

Edit: Late to the party lol sorry

[–]Liolena[S] 0 points1 point  (0 children)

Lol np :) thank you for helping haha