Replacing DOM elements in JavaScript

A simple example of a replacing DOM elements in JavaScript.
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
    <meta name="description" content="Tizen example"/>
    
    <title>Tizen example</title>

</head>

<body>

<div id="sampleDiv">
<p id="par_1">Sample paragraph</p>
<p id="par_2">The second sample paragraph</p>
</div>

<script>

var header = document.createElement("h3");
var node = document.createTextNode("We are putting this instead of the last one!");

header.appendChild(node);

var element = document.getElementById("sampleDiv");
var child = document.getElementById("par_2");
element.replaceChild(header, child);
</script>

</body>
</html>

Responses

0 Replies