eventListener on click
I'm dealing with a "simple issue". Basically, i want to put my code inside a function instead of passing directly in the button, but i'm having a issue where i don't know exactly how i'm supposed to do it, so. Here's the html, very simple:
the js:
34 Replies
I do believe i'm overthinking about something more simple than it seems to solve
you're not only overthinkinging but you're misuing it
you can delete your entire javascript and just add the
required
attribute to the fields
BUT!!!!!
you have to put everything inside a form, as you should have done from the beginning
and the button should have the type explicitly set to submitoooh
Definitely what epic said, but to answer the actual question, when you're passing in a function like to an event listener, you need to omit the calling brackets where you pass the function reference.
So this
i tried without but still wasn't working
so this made me have a little mentalbreakdown for a few seconds
if was it wrong or not
since the logic is right and is just to show a simple alert
you have 2 different clicks
it was just an example of the right version, but once i do like this
doesn't work
idk
can be like bcs of the order in the script?
By adding the (), you're telling Javascript to run the function and pass the result to the listener. The event listener should do the calling, so you just pass it the reference to the function to run
like the function going after
or ?
this
Make a codepen or something then, I can't run code in my head
ok wait a sec
what jochem said is supposed to work
I'm on mobile atm though so I can't look too much further. I'll look when I can if epic hasn't solved it already 🙂
you already solved it
checkButton.addEventListener("click", checkIsEmpty)
<-- this is the solution