Intersection Observer error
Hi, I'm a UX designer trying to get that hang of coding in Vue JS. I wouldn't say it's going great, so here I am asking for some assistance to solve my current issue!
Code in question;
I've tried multiple different videos, there's also been times where it works only one time for some reason and refuses to do anything after each refresh. Now I keep getting the aforementioned error, looking online didnt give me much of an answer that I understood.
Thanks!
62 Replies
most likely,
document.querySelector('.hidden')
isn't returning anythingIs something wrong here then?
is the script in the <head> or at the end of the <body>?
At the end of the body
use the console log and you'll see
console.log(movingCarSection)
after the queryselectornull
is that element created by another script or something?
are you sure the classname is correct?
I'm sure, the only two instance of "hidden" are the within the element in the image above & the query selector
No
can you show the full html then?
not from the inspect element but from the source code
Its like 1200 lines
then do ctrl-f and find it in the code
Oh you mean it like that, my bad
Hold on
A little tip on the side: The classname "hidden" is quite generic. Maybe consider giving it a more describing name. Like ".movingcar" or someting
or use the id
Svg is like this just to see if I can get it working at all, if it causes the issue, hopefully not.
ah, it is an id. your queryselectorr is wrong, instead of a dot, use the hashtag #
document.querySelector("#hidden");
he's selecting by the class
Gotcha, yeah I've been looking at various tutorials from different creators and websites, seeing if anything works lol
5 hours later
ohw sorry, my mistake
that's not the generated html
it should have been okay, i was focussing on the id
Im not sure what you mean then
sorry
ctrl+u > ctrl+a > ctrl+c > alt+tab > ctrl-v
view source, select all, copy, alt-tab, paste
I can't show everything in my code, half of it would bring me in trouble 😅
I can do the script though
that doesnt help at all
the generated html is extremely important
without it, its speculation, guesses and little more than sprinkling magic dust
if the web page is accessible online, then it helps too
no it's not.
As long as we know the element is loaded in the dom before the script runs, it's fine. So please inspect for yourself (f12) if the car element is on the page before that script
As long as we know the element is loaded in the dom before the script runs, it's fine. So please inspect for yourself (f12) if the car element is on the page before that script
you dont
you dont know anything because we have little to nothing
i can reproduce that problem with 4 lines of code
or even 3
that should be enough to trigger the error
so, no full html, or a minimal subset of that html that triggers the error, we have nothing to work off of
OP is not willing to share all of the code (which I can totaly understand if it is workrelated) . You should not ask for that. Only the relevant part helps. Your example is totaly irrelevant as well. We already asked if the scripts was loaded after the html was loaded in the dom. We have seen the 2 different parts, only thing we're not sure of is in what order it's on the page (OP can check that themselves).
its fine if he doesnt want to share the code
which is why i asked a small subset that triggers it, or a link where we can see the full generated code if it exists
both of which arent trade secrets or anything because your browser receives that
but without a real (trimmed or untrimmed) html document, everything else is speculation
im just asking a copy of everything/part that the browser receives when the page is accessed by the browser
if that is trade secret as well, its the end of the road
ive shown you how to reproduce the same problem in 4 lines of html+js
and it fits the entire description of what was shared
without the html the browser receives, the best i can do is to tell you to wrap everything in a ready event handler
something like this:
Sorry, asked a colleague that doesnt know js just to be sure. He doesnt understand it either
Let me read the messages, sorry
Yeah, it's work related! It's gonna be public eventually though there's multiple calls to our servers etc, and some other stuff that's pretty sensitive
then when you view the source that the browser receives, can you find the element there?
What does
document.querySelector('.hidden')
return in the browser console?Null
Changed hidden to henk, just to see if that was the issue
I assume this is before you changed it to class="henk"?
Before and after
thats the element inspector
queryselector('.henk')?
not the source that's sent to the browser
Yeah I just changed the name to see if that worked, but it did not
that's really bizarre, I mean, the class="henk" is right there on the screen and
document.querySelector('.henk')
in the console should 100% find that elementcan you uncollapse the <head> tag for us
you're using the dev tools console on that page, right?
Yeah
and your script is just before the closing </body> right?
It's in the app.vue file, so just after </main></template>
Its in <div id="app"></div>
okay, here it gets in the vue territory, i have to pull out. not enough experience with it.
Gotcha, really appreciate the help
It's likely your app isn't loaded yet at that point. Your browser will hit the script tag immediately after loading the div#app, which won't have Vue mounted yet
Does mounted() {} play a role here?
I've tried onmounted, it didnt seem to work
Ill have a look
it's been an Age since I used vue, just after they implemented the Composition api
and the main.js is what has that js code you sent in the first message?
if it is, you need to move the vite code before that script
Appreciate the help guys, work day is over, too frustrated too continue figuring it out 🫡
Enjoy your weekend
If this is entirely inside the vue app, you can use a
ref
instead. Add ref="movingCars"
to your element, and then in your mounted()
hook, you can use this.$refs.movingCars
instead of your query selector- this is assuming you're using options API though.
Composition API you'd have to make a const movingCars = ref(null);
and then add your ref="movingCars"
to your element. Then in the onMounted()
hook you should be able to access the referenced element.
I would be interested to see if that changes anything.The official docs have some good examples for this: https://vuejs.org/guide/essentials/template-refs.html
Template Refs | Vue.js
Vue.js - The Progressive JavaScript Framework
Hi, I did not see your message. But you're right, its entirely in the Vue app. I'll have a look and see if it works.
Thank you