Solved

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

Posted on 2004-09-28
3
239 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Possible propagation problem 1 34
testing a web application for ipad on windows pc 3 34
Triple line in an alert message 4 19
15 puzzle in Javascript/html 2 35
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

685 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