Solved

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

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

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 …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

688 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