Element children Property - Change the background color of the second child element of a <div> element: - Javascript DOM

Javascript examples for DOM:Element children

Description

Element children Property - Change the background color of the second child element of a <div> element:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
div {/*from w w  w.  j a  v  a  2 s .co 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").children;
    c[1].style.backgroundColor = "yellow";
}
</script>

</body>
</html>

Related Tutorials