Jump to content

Remove Element With Dom


blob84
 Share

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...