Click to See Complete Forum and Search --> : DOM add an image


tdh1985
10-09-2005, 06:31 PM
HI,
I'm wanting to add an image to a DIV tag when the user clicks a button, I have managed to create some text this way and was wondering if the same technique could be used to add an image?

Thanks.

{
newelement=document.createElement("p");
// this sets the new elements id to newpara
newelement.setAttribute("id","newpara");
//creates a textnode with some text in it
newtext=document.createTextNode("The new text");
newelement.appendChild(newtext);
document.body.appendChild(newelement);
}

tabzter
10-09-2005, 07:22 PM
Use the following Code:

var myDIV=document.getElementById("divID");
var newElem=document.createElement("IMG")
newElem.setAttribute("title","set image title here eg. Me and my mate");
newElem.setAttribute("src","set filename here eg myImg.jpg");
myDIV.appendChild(newElem);


Make sure that you have a <div> element defined as follows somewhere in your document for the above code to work:

<div id="divID" ...>
...
</div>