views:

138

answers:

4

How can i get the background color of any element, say Div, using javascript. I tried:-

<html>
    <body>
     <div id="myDivID" style="background-color: red">shit happens</div>
     <input type="button" value="click me" onclick="getColor();">
    </body>

    <script type="text/javascript">
     function getColor(){
      myDivObj = document.getElementById("myDivID")
      if ( myDivObj ){
       alert ( 'myDivObj.bgColor: ' + myDivObj.bgColor ); // shows: undefined
       alert ( 'myDivObj.backgroundcolor: ' + myDivObj.backgroundcolor ); // shows: undefined
       //alert ( 'myDivObj.background-color: ' + myDivObj.background-color ); // this is not a valid property :)
       alert ( 'style:bgColor: ' + getStyle ( myDivObj, 'bgColor' ) ); //shows: undefined
       alert ( 'style:backgroundcolor: ' +  getStyle ( myDivObj, 'backgroundcolor' ) ); // shows:undefined:
       alert ( 'style:background-color: ' +  getStyle ( myDivObj, 'background-color' ) );  // shows: undefined
      }else{
       alert ( 'damn' );
      }
     }
     /* copied from `QuirksMode`  - http://www.quirksmode.org/dom/getstyles.html - */
     function getStyle(x,styleProp)
     {
      if (x.currentStyle)
       var y = x.currentStyle[styleProp];
      else if (window.getComputedStyle)
       var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
      return y;
     }
    </script>
</html>
+7  A: 

As with all css properties that contain hyphens, their corresponding names in JS is to remove the hyphen and make the following letter capital: backgroundColor

alert(myDiv.style.backgroundColor);
David Hedlund
Damn! that was so easy :)
Rakesh Juyal
David will you please let me know why `getStyle` is used in `http://www.quirksmode.org/dom/getstyles.html`, when it was so easy to get the style property.
Rakesh Juyal
well the page you link to has some descriptions in itself as to what it's good for. for mozilla, for instance, it uses `getComputedStyle`, which is not so much what's specified in the stylesheet, but rather, what happens to be displayed, both as a result of HTML markup *and* CSS styling. For something as simple as this scenario, though, I don't see any really good reason to use that function.
David Hedlund
The `style` property only contains styles assigned in a `style` attribute or set by scripting. Styles set in a `style` element or an external stylesheet won't be found there, at which point you need different techniques for different browsers (standard techniques for everything but IE, as usual) which ppk's `quirksmode` script will take care of.
NickFitz
+1  A: 

Using JQuery:

var color = $('#myDivID').css("background-color");
Aziz
the div before the id selector is a little redundant
AutomatedTester
I agree, fixed :)
Aziz
downloading and executing a 20kb library for retrieving the background color of a DIV is a little redundant ;)
David Hedlund
@David: sorry david, though i wanted to give you +20 reps but SO is not allowing me to do so :). +1 for you. Agree altogether.
Rakesh Juyal
well, if the only javascript used in the whole page is to get the background color, then it is redundant, but usually a page has much more things done in javascript, which makes using JQuery a little reasonable.
Aziz
+2  A: 

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;

nemisj
Thanks nemisj :) +1
Rakesh Juyal
A: 

with jQuery:

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

with prototype:

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

with pure JS

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