By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,702 Members | 1,760 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,702 IT Pros & Developers. It's quick & easy.

why does getting style.color or an element return nothing?

P: n/a
<html>
<head>
<style type="text/css">
#cssdata{color:blue;}
</style>

<script language="Javascript" defer="defer">

function test(){
alert(cssdata.style.color)

alert(test2());

}
function test2(){
x = document.getElementById('cssdata');
return x.style.color;

}
</script>
</head>
<body>
<span id="cssdata">  hello </span>
<div class="test" onclick="test()">testing 2
</div>
</body>
</html>
returns two empty alerts in IE version 6.0 xpsp2

I have also tried without a defer attribute of course.

cssRules is undefined.

Aug 24 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a


pantagruel wrote:

<style type="text/css">
#cssdata{color:blue;}
</style> function test2(){
x = document.getElementById('cssdata');
return x.style.color; <span id="cssdata">  hello </span>


element.style represents the inline style of an element set in the HTML
style attribute or with script thus if you have
<span style="color:blue;">
then element.style.color will give you a color value (might be 'blue'
but a browser also give you the color as an rgb triplet) but in your
example you get the empty string as the inline style is empty.

If you want the computed CSS value then you need a different scripting
approach using getComputedStyle for Mozilla and Opera and currentStyle
for IE.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 24 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.