Replace text in an HTML element using replaceChild method - Javascript DOM

Javascript examples for DOM:Element replaceChild

Description

Replace text in an HTML element using replaceChild method

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){// w  w  w. ja v  a2s .c om
var td1 = document.getElementById('Fruit'),
    text = document.createTextNode("Apple");
td1.replaceChild(text,td1.firstChild);

    }

      </script> 
   </head> 
   <body> 
      <span id="Fruit">Banana</span>  
   </body>
</html>

Related Tutorials