r/JavaScriptTips • • Jan 05 '22

Code to trigger all onclick() in a page

I'm trying to make a code to put in the browser console to trigger all onclick events of a page.
I have this:

But for some reason it only triggers once, does anyone know why?

4 Upvotes

2 comments sorted by

3

u/whamjayd Jan 05 '22

Ok, so I had to put a:

await new Promise(r => setTimeout(r, 500));

and now it works, I'll leave the post up in case someone comes across the same problem

2

u/whamjayd Jan 05 '22

More info:
the code is entering the if statement 106 times (the right amount) but it is only triggering the last element of the list created by querySelectorAll