Jump to content

Remove Element With Dom


blob84

Recommended Posts

I have this codei want to remove all elements inside the divmy function closeDiv doesn't work :

<html><head><script "text/javascript">function closeDiv(){  if(this.createDiv)  {	document.getElementById("prince").removeChild(createDiv);	createDiv = null;  }}function insert() {			if(document.getElementById && document.createElement) {		createDiv = document.createElement("div");createDiv.setAttribute("id", "div1");document.getElementById("prince").appendChild(createDiv);createDiv = document.createElement("div");createDiv.setAttribute("id", "div2");document.getElementById("div1").appendChild(createDiv);img=document.createElement("img");img.setAttribute("alt", "slide");	img.setAttribute("src", "http://www.volalibero.it/immagini/colorizzare_1.jpg");document.getElementById("div2").appendChild(img);}}</script></head><body id="prince"><input type="button" value="00button" onclick="insert();" /><input type="button" value="01button"  onclick="closeDIv();" /></body></html>

Link to comment
Share on other sites

element.removeChild() can only remove an immediate descendent. Your function is trying to remove the child of a child. That won't work. Div1 is the parent of Div2.A nifty trick in a case like this is to use relative references:var el = document.getElementById("div2");el.parentNode.removeChild(el);or even:function removeElement (el) {el.parentNode.removeChild(el);}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...