Javascript DOM Element Replace Child

Description

Javascript DOM Element Replace Child

View in separate window


<html>
<head>
    <title>Replacing Nodes</title>
</head>//  w  w w  . j  av a2  s.c  om
<body>
    <div>
        <p id="myParagraph">This is a test</p>
    </div>

  <script type="text/javascript">
    let newParagraph = document.createElement("p");
    newParagraph.innerText = "I'm a new node!";
    newParagraph.textContent = "I'm a new node!";
  
    let oldParagraph = document.getElementById("myParagraph");
    oldParagraph.parentNode.replaceChild(newParagraph, oldParagraph);
  </script>
</body>
</html>



PreviousNext

Related