views:

43

answers:

3

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...

+1  A: 

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

So...

alert(document.getElementById("myMenu").className);
gmcalab
+1  A: 

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 Wizard