0

If I set a style inline like this:

<div id='myMenu' style='background-color:red'></div>

Then in JS call

alert("document.getElementById('myMenu')style.backgroundColor");

The result alert box would report "red"

However if I set the style internally in the via a class or id

<style type='text/css'>    
.menu {
       background-color:red;
    }
</style>

Then the alert reports blank.

Will styles with .getElementById only work with inline style? Seems very limiting...

Gabe
  • 49,577
  • 28
  • 142
  • 181
sbuck
  • 1,846
  • 4
  • 24
  • 40
  • Setting the style in a style tag should work just fine. Can you post the actual code you are trying to use? (remember that "." is for class names, and '#' for ID's) – scunliffe Oct 25 '10 at 19:07
  • 1
    See http://stackoverflow.com/questions/1098349/reading-non-inline-css-style-info-from-javascript – Roatin Marth Oct 25 '10 at 19:13

2 Answers2

1

You are not setting the style attribute for the element but instead you are setting a class.

So...

alert(document.getElementById("myMenu").className);
Gabe
  • 49,577
  • 28
  • 142
  • 181
1

You can use jQuery to read the "real" background color, no matter how it was assigned:

alert($("#myMenu").css("background-color"));

No idea how it's doing it, but I've checked it now and it works. :)

You can download most recent version of jQuery from the official site: http://docs.jquery.com/Downloading_jQuery

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208