Javascript DOM CSS Style backgroundColor Property set for <div>

Introduction

Set a background color of a specific <div> element:

document.getElementById("myDiv").style.backgroundColor = "lightblue";

Click the button to set the backgroundColor property of the DIV element to lightblue:

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
#myDIV {/*from   w  ww . j  a v a2  s .  co  m*/
  width: 300px;
  height: 300px;
  background-color: coral;
  color: white;
}
</style>
</head>
<body>
<button onclick="myFunction()">Test</button>

<div id="myDIV">
  <h1>Hello</h1>
</div>

<script>
function myFunction() {
  document.getElementById("myDIV").style.backgroundColor = "lightblue";
}
</script>

</body>
</html>



PreviousNext

Related