Element childNodes Property - Change the background color of the second child node (index 1) of a <div> element: - Javascript DOM

Javascript examples for DOM:Element childNodes

Description

Element childNodes Property - Change the background color of the second child node (index 1) of a <div> element:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
div {/*from   w  w w. ja va2  s  .c  o m*/
    border: 1px solid black;
    margin: 5px;
}
</style>
</head>
<body>

<button onclick="myFunction()">Test</button>

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

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

</body>
</html>

Related Tutorials