How to Change Background Color of Document using JavaScript onclick ?

<html>
<head>
<script type="text/javascript">
<!--
function changeBGC(color){
document.bgColor = color;
}
//-->
</script>
</head>
<body>
<a href="#" onClick="changeBGC('#FF0000')">Background: Red</a><br/>
<a href="#" onClick="changeBGC('#00FF00')">Background: Green</a><br/>
<a href="#" onClick="changeBGC('#0000FF')">Background: Blue</a>
</body>
</html>

Explanation :

function changeBGC(color)
{
document.bgColor = color;
}
[arrowlist]
  • Function changeBGC() will change background color of the page.
  • This function is triggered by clicking on the link.
  • When link is clicked onclick event is triggered by calling function.
[/arrowlist] Syntax of Calling JS Function :

<a href="#" onClick="changeBGC('#FF0000')">Background: Red</a>