Hi I have a problem with event listener, I am using turbolinks my listener looks like:
document.addEventListener("turbolinks:load", ...
when I for the first time visit the page i get in browser console this error:
Uncaught TypeError: Cannot read property 'children' of null
. After reloading everything is working properly. I guess that this is the problem with asynchronicity but i don't know how to solve that in wise way.
Asked
Active
Viewed 420 times
0

Bartosz Bronikowski
- 35
- 7
-
what is the problem you're trying to solve with your event listener? it is entirely possible that there's another way that avoids this error. – aidan Aug 18 '21 at 00:19
-
I use event listener to switch classes in css after clicking on selected divs and some things are not loaded for the first time I visit the site due to asynchronicity. – Bartosz Bronikowski Aug 18 '21 at 01:45
-
I would highly recommend using Stimulus for your Javascript needs here. Stimulus is meant to work with Hotwire. This doesn't answer your question exactly, but if you ask a new question about changing CSS on click with Hotwire/Turbo/Stimulus I can provide a sample. See: https://stimulus.hotwire.dev/ – aidan Aug 20 '21 at 16:00
-
Ok thanks I wiil try stimulus then :) – Bartosz Bronikowski Aug 22 '21 at 22:07