• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

14. Creating Dynamic Content > 14.15. Removing Body Content

Removing Body Content

NN 6, IE 5

Problem

You want to eliminate an element or portion of text from the current document.

Solution

If you have a reference to the element you wish to delete, you can use the W3C DOM removeChild( ) method to remove the element. The method works only on child nodes, so you must step outward to the element’s parent to invoke the method:

var elem = document.getElementById("spanToGo");
elem.parentNode.removeChild(elem);

To eliminate text from a text node, set its node value to an empty string:

var container = document.getElementById("someSpan");
// verify that the child node is a text node before emptying it
if (container.firstChild.nodeType =  = 3) {
    container.firstChild.nodeValue = "";
}

PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint