Problem with addeventlistener
Could someone explain why this is making the function run before any button has been pressed? Should this not wait until deletePostBtn has been clicked before running deletepost()?
6 Replies
because you are calling the function when passing it in. So it is running when it creates the listener..
deletePostBtn.addEventListener("click", () => {deletePost()})
You could wrap it in a function/arrow funcThanks man, i think i tried that but it didnt work. Turns out i was just having issues with button it self. Im making it in js so i was trying to select it before it was created 🙃
use
defer
in your <script> import😄
anytime the JS needs your DOM, or ran after page load.if you don't need to pass params you could do it without the calling it too. (I typically just always wrap it so I can access the event.)
deletePostBtn.addEventListener("click", deletePost)
Ohhh thanks! I'll read up on that