But it fails, the elements won’t display, and usually even without any error message. Just can’t figure out why because when I verify the variables most of them contain what’s expected:
[CODE]
var d=document.createElement(“div”);
var j=0;
var height=window.innerHeight/255;
function div(){
for(i=0;i<400;i++) {
d.setAttribute(“style”,”margin:0px”);
d.style.width=”100%”;
d.style.height=height;
d.style.border=”1px solid black”;
}
document.body.appendChild(d);
}
onload=div;
Variable “d” exists only outside the funtion declaration. When I type in “alert(d)” inside the funtion nothing happens. But when I do it prior to or after the function block it works. So this seems to be a variable scope issue (Maybe I’ve forgotten some) although I can’t see how or why; the variables are global and must therefore be accessible within the function div() as well, isn’t that so?
Not only that, but the 400 div elements that are created within the loop don’t display yet there’s no error messages indicating any error. I even tried to verify by using “alert(d.getAttribute(“style”)”, and it worked; all the properties were displayed. Problem is there’s either only one div or none.
[COLOR=DarkRed]What am I doing wrong here? Something obvious to you guys perhaps?
I’m a DOM2 beginner. It looks so easy and logical though, yet all these problems…If the div element exisists within the code, why’s not appeneded to the page with document.body.appendChild(d)?)