Edit This Code:
<!DOCTYPE html>
<html>
<head>
<style>
div {
    border: 1px solid black;
    margin: 5px;
}

</style>
</head>
<body>

<p>Click the button to add a background color to the second child node (index 1) of div.</p>

<button onclick="myFunction()">Try it</button>

<div id="myDIV">
  <p>First p element</p>
  <p>Second p element</p>
</div>

<p><strong>Note:</strong> Whitespace inside elements is considered as text, and text
is considered as nodes. In this example, index 0, 2 and 4 in DIV are text nodes.</p>

<script>
function myFunction() {
    var c = document.getElementById("myDIV").childNodes;
    c[1].style.backgroundColor = "yellow";
}
</script>

</body>
</html>


Result:
Try it Yourself - © w3schools.com
Privacy Policy