Get the background color of a specific <div> element: - Javascript CSS Style Property

Javascript examples for CSS Style Property:backgroundColor

Description

Get the background color of a specific <div> element:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<h1>Hello World!</h1>

<div id="myDiv" style="background-color:orange;">This is a div element.</div>
<br>
<button type="button" onclick="myFunction()">Get the background color of div</button>

<script>
function myFunction() {/*from w  w w  .j  a  va 2  s.c o m*/
    console.log(document.getElementById("myDiv").style.backgroundColor);
}
</script>

</body>
</html>

Related Tutorials