How to Change Background Color Using JavaScript

In the below given example, we are changing background color of DIV tag by using its ID on click of a button.

<html>
<head>
<title>JavaScript Tutorial </title>

<script type="text/javascript">

function bgChange()
{
    document.getElementById("myDiv").style.backgroundColor = "red";
}

</script>
</head>
<body>
 
<input type="button" value="ClickMe" onclick="bgChange()"/>

<div  id="myDiv" >

Hello World!
 
</div>

 
</body>
</html>

 

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •