Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

why doesn't document.appendChild(anElem) work?

I create in element using createElement(), but I can't append it to the document element, only divs inside said doc, source below:

var shadowDiv = document.createElement("div");
var shadowImg = document.createElement("img");

shadowDiv.id = "shadowDiv";
shadowImg.id = "shadowImg";

shadowDiv.visibility = "visible";            
shadowDiv.style.overflow = "hidden";
shadowDiv.style.position = "absolute";
shadowDiv.style.left = "0px";
shadowDiv.style.top = "0px";
shadowDiv.style.width = "167px";
shadowDiv.style.height = "175px";
shadowDiv.zindex = "1";

shadowImg.src = "/images/something.png";
shadowImg.width = "167";
shadowImg.height = "175";

shadowImg = shadowDiv.appendChild(shadowImg);
//shadowDiv = document.appendChild(shadowDiv); //doesnt work
shadowDiv = document.getElementById("listRow1").appendChild(shadowDiv); //does work
0
sethUSer420
Asked:
sethUSer420
  • 2
1 Solution
 
cLFlaVACommented:
Why not append it to the body tag?

shadowDiv = document.getElementById("bod").appendChild(shadowDiv);

<body id="bod" ...>
...
...
0
 
cLFlaVACommented:
From the MSDN specifications on appendChild:


To display new elements on the page, you must append them within the body element. For example, the following syntax demonstrates how to add a div element to the body.

var oDiv=document.createElement("DIV");
document.body.appendChild(oDiv);
0
 
ZvonkoSystems architectCommented:
Is the element with the ID listRow1 an element of type TR?
Then you cannot append a div.
Put some id to the element of type TD and append there your DIV child.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now