Developers Web home

Remove an Element from the page, safely

To remove an html element (including all child elements) from the pages body, we need to call the 'removeChild' DOM function. But to do this, we need to work from the parent object of the element to remove.

function removeTheNode(id) {       // id = the elements id to remove
if (document.getElementById(id)!=null) {       // ensure the element exists
thisNode=document.getElementById(id);       // find the element object
thisNode.parentNode.removeChild(thisNode);       // remove the child from its own parent
}
}
No conversation on this topic You must be logged in to add to the conversation