I have a function that should be simple enough and should be executed after loading in order to reduce the initial loading time.
I mainly use this code to get all the elements with the prefImg class and do something with them. But when debugging, firebug says that var divsList is undefined.
function populatePrefsList() { var divsList = new Array(); divsList = document.getElementsByClassName("prefImg"); var x = divsList.length; var i = 0; for(i=0; i<divsList.length; i++) { var imgs = divsList[i].getElementsByTagName("img"); var imgSRC = imgs[0].src; var alt = imgs[0].alt; var descs = divsList[i].getElementsByTagName("h4"); var desc = descs[0].innerHTML;
Obviously, I have a breakpoint on var x = divsList.length ...
I do not understand this, I initially had a script in the Head of the page, but, believing that he may not have loaded the divs yet, moved it to the bottom of the Body. It didn’t decide.
I had var divsList = document.getElementsByClassName("prefImg");
If someone tells me where I did wrong, I would be grateful. There are about 50 divs with the class prefImg .
Greetings
source share