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

P

pantagruel

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

Martin Honnen

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top