<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: replaceChild code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Wed, 23 Jul 2008 19:50:07 GMT</pubDate>
    <description>DZone Snippets: replaceChild code</description>
    <item>
      <title>javascript - dom method for replacing nodes</title>
      <link>http://snippets.dzone.com/posts/show/2599</link>
      <description>In addition to removing nodes, the DOM offers a way to replace one node with another. The replaceChild method accomplishes this. As with removeChild above, replaceChild needs to be called from the element that contains the node you wish to replace.&lt;br /&gt;&lt;br /&gt;replaceChild takes two arguments: a reference to the new node, and another to the node being replaced. The following example creates a new SPAN element containing a text node, and uses it to replace an existing SPAN. &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;script type="text/javascript"&gt;&lt;br /&gt;function replaceSpan(){&lt;br /&gt;&lt;br /&gt;    var newSpan = document.createElement("span");&lt;br /&gt;    var newText =&#172; &lt;br /&gt;	document.createTextNode("on top of the astounded zebra");&lt;br /&gt;    newSpan.appendChild(newText);&lt;br /&gt;&lt;br /&gt;    var para = document.getElementById("example3");&lt;br /&gt;    var spanElm = document.getElementById("ex3Span");&lt;br /&gt;    var replaced = para.replaceChild(newSpan,spanElm);&lt;br /&gt;}&lt;br /&gt;&lt;/script&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;p id="example3"&gt;The quick brown fox jumps &lt;span id="ex3Span"&gt;over the lazy dog&lt;/span&gt;.&lt;/p&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;button onclick="replaceSpan();"&gt;Call replaceSpan()&lt;/button&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 15 Sep 2006 22:53:32 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2599</guid>
      <author>bradalyst (brad)</author>
    </item>
  </channel>
</rss>
