[javascript] How to get the background color of an HTML element?

How can I get the background color of any element, like a div, using JavaScript? I have tried:

_x000D_
_x000D_
<html>_x000D_
_x000D_
<body>_x000D_
  <div id="myDivID" style="background-color: red">shit happens</div>_x000D_
  <input type="button" value="click me" onclick="getColor();">_x000D_
</body>_x000D_
_x000D_
<script type="text/javascript">_x000D_
  function getColor() {_x000D_
    myDivObj = document.getElementById("myDivID")_x000D_
    if (myDivObj) {_x000D_
      console.log('myDivObj.bgColor: ' + myDivObj.bgColor); // shows: undefined_x000D_
      console.log('myDivObj.backgroundcolor: ' + myDivObj.backgroundcolor); // shows: undefined_x000D_
      //alert ( 'myDivObj.background-color: ' + myDivObj.background-color ); // this is not a valid property :)_x000D_
      console.log('style:bgColor: ' + getStyle(myDivObj, 'bgColor')); //shows: undefined_x000D_
      console.log('style:backgroundcolor: ' + getStyle(myDivObj, 'backgroundcolor')); // shows:undefined:_x000D_
      console.log('style:background-color: ' + getStyle(myDivObj, 'background-color')); // shows: undefined_x000D_
    } else {_x000D_
      console.log('damn');_x000D_
    }_x000D_
  }_x000D_
  /* copied from `QuirksMode`  - http://www.quirksmode.org/dom/getstyles.html - */_x000D_
  function getStyle(x, styleProp) {_x000D_
    if (x.currentStyle)_x000D_
      var y = x.currentStyle[styleProp];_x000D_
    else if (window.getComputedStyle)_x000D_
      var y = document.defaultView.getComputedStyle(x, null).getPropertyValue(styleProp);_x000D_
    return y;_x000D_
  }_x000D_
</script>_x000D_
_x000D_
</html>
_x000D_
_x000D_
_x000D_

This question is related to javascript html background-color

The answer is


This worked for me:

var backgroundColor = window.getComputedStyle ? window.getComputedStyle(myDiv, null).getPropertyValue("background-color") : myDiv.style.backgroundColor;

And, even better:

var getStyle = function(element, property) {
    return window.getComputedStyle ? window.getComputedStyle(element, null).getPropertyValue(property) : element.style[property.replace(/-([a-z])/g, function (g) { return g[1].toUpperCase(); })];
};
var backgroundColor = getStyle(myDiv, "background-color");

It depends which style from the div you need. Is this a background style which was defined in CSS or background style which was added through javascript(inline) to the current node?

In case of CSS style, you should use computed style. Like you do in getStyle().

With inline style you should use node.style reference: x.style.backgroundColor;

Also notice, that you pick the style by using camelCase/non hyphen reference, so not background-color, but backgroundColor;


Using JQuery:

var color = $('#myDivID').css("background-color");

Simple solution

myDivObj = document.getElementById("myDivID")
let myDivObjBgColor = window.getComputedStyle(myDivObj).backgroundColor;

Now the background color is stored in the new variable.

https://jsfiddle.net/7q1dpeo9/1/


With jQuery:

jQuery('#myDivID').css("background-color");

With prototype:

$('myDivID').getStyle('backgroundColor'); 

With pure JS:

document.getElementById("myDivID").style.backgroundColor

Get at number:

window.getComputedStyle( *Element* , null).getPropertyValue( *CSS* );

Example:

window.getComputedStyle( document.body ,null).getPropertyValue('background-color');  
window.getComputedStyle( document.body ,null).getPropertyValue('width');  
~ document.body.clientWidth

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment

Examples related to background-color

SwiftUI - How do I change the background color of a View? How to add a color overlay to a background image? How to change the background color on a input checkbox with css? How to change DatePicker dialog color for Android 5.0 Set Background color programmatically How to change the background-color of jumbrotron? Set textbox to readonly and background color to grey in jquery Change the background color of a pop-up dialog Background color of text in SVG Change background color of iframe issue