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

14. Creating Dynamic Content > 14.14. Replacing Portions of Body Content

Replacing Portions of Body Content

NN 6, IE 5

Problem

You want to replace content in the document with dynamically generated content.

Solution

There are different tactics, depending on whether the content to be replaced is simply the text content of an element or a series of HTML elements. To replace all text content inside an element, create a text node and replace the container’s current child node with the new one:

var txt = document.createTextNode("Every good boy does fine.");
var elem = document.getElementById("someElement");
var oldTxt = elem.replaceChild(txt, elem.firstChild);

When the element’s current content contains both text and interlaced elements (such as a paragraph element containing a portion of text marked up as an em element), delete each nested node prior to inserting the new content:


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


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