Change the background color of the second child element of a <div> element:
Click the button to add a background color to the second child element (index 1) of div.
<!DOCTYPE html> <html> <head> <style> div {//from w ww .j a v a2s. 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>