0

I try to loop through all the nodes in a div. I use this code to have access to all the nodes in the div.

var div = document.getElementById('thedivId');
var childs = div.childNodes

and then i try to loop through it using childs.length

but in the console when i run it i am getting this error:

Cannot read property 'childNodes' of null

I cant understand why this happens, I know that isn't an array, but the node list has the property length, and i have seen it used in here: http://www.w3schools.com/jsref/prop_node_childnodes.asp

Andy
  • 39,764
  • 8
  • 53
  • 80
Chris
  • 7
  • 1

1 Answers1

0

The most probably answer is that when you run that, DOM is not ready, as Ismael said above

dpaul1994
  • 338
  • 2
  • 15