Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-09-28
3
Medium Priority
?
243 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…
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…

596 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