Javascript Style How to - Set background color








Question

We would like to know how to set background color.

Answer


<!DOCTYPE html>
<html>
<head>
<style>
#test {<!--from  w  w w.  j  a  v  a 2s .  c  o  m-->
  background: red;
  width: 30px;
  height: 30px;
}
</style>
</head>
<body>
  <div id="test"></div>
  <script>
            var a = document.getElementById('test');
            a.style.background = "#00f";
    </script>
</body>
</html>

The code above is rendered as follows: