How to use JavaScript to change div backgroundColor

var div = document.getElementById( 'div_id' );
div.onmouseover = function() {
  this.style.backgroundColor="green";
  var h2s = this.getElementsByTagName( 'h2' );
  h2s[0].style.backgroundColor="blue";
};
div.onmouseout = function() {
  this.style.backgroundColor="transparent";
  var h2s = this.getElementsByTagName( 'h2' );
  h2s[0].style.backgroundColor="transparent";
};

Leave a Comment