Skip to content
Advertisement

Javascript DOM capturing after using javascript to insert dom

I have a question regarding DOM manipulation, say I insert a grid using this function:

JavaScript

afterwards, I try to grab all the vertexes with the class “vertex” using document.querySelectorAll(“vertex”) it doesnt work:

JavaScript

console log of this returns:

JavaScript

but this works:

JavaScript

console.log of this actually returns all 9 divs

I see that the difference is that grid__container is pre-defined in my HTML file, but I wrote the querySelectorAll method in a top down approach, so I dont understand why query selector doesnt work, because I created the elements then tried to grab it, anyone knows why?

I only know this is called DOM-manipulation and not know the exact name of this error and am quite new to javascript, please help.

I expected the console to log out 9 divs, but my vertexes here using querySelectorAll instead logs out a nodelist of length 0 <NodeList: length 0>

JavaScript
JavaScript
JavaScript

Advertisement

Answer

Your code works if you spell everything correctly and run the code AFTER the DOM is complete AND get the vertexes at the time you need them

Also the .mole is not available until after the randomSquare has run

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement