Solved

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

Posted on 2004-09-28
3
235 Views
Last Modified: 2008-03-06
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
Comment
Question by:sethUSer420
  • 2
3 Comments
 
LVL 13

Expert Comment

by:cLFlaVA
ID: 12169632
Why not append it to the body tag?

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

<body id="bod" ...>
...
...
0
 
LVL 13

Accepted Solution

by:
cLFlaVA earned 250 total points
ID: 12169662
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
 
LVL 63

Expert Comment

by:Zvonko
ID: 12169801
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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now