views:

208

answers:

5

Hello,

Here's a very basic question: why is the finishLoading() function in the code below not able to access the 'opacity' property for the #myStyle CSS selector? The alert doesn't display anything, and I've verified that the 'opacity' property is 'false'.

Thanks very much!

<html>
<head>
<style type="text/css">
<!--
#myStyle
{
    opacity: 0.50;
}
-->
</style>

<script type="text/javascript">
<!--
function finishedLoading()
{
    alert(document.getElementById('myStyle').style.opacity);
}
-->
</script> 
</head>
<body onload="finishedLoading();">

    <div id="myStyle">
        hello
    </div>

</body>
</html>
A: 

I suggest you take a look at jQuery and some of the posts at Learning jQuery, it will make doing things like this very easy.

Tom
e.g. $('#myStyle').css("opacity","0.8")
Fermin
While I'm happy to use jQuery or whatever else will get the job done, I'd like to understand what concept it is that I'm missing which explains the behavior I'm seeing. Perhaps you could tell me what specific areas of HTML/CSS/Javascript I should be researching?
jQuery is cool, but it is just an abstraction of the function phoenix explains in his answer.
Boldewyn
A: 

Opacity should be a number rather than a boolean. Is it working in any other browseR?

Fermin
Ah, sorry, I was unclear. I would expect a float/number when attempting to access the opacity property, but it doesn't seem the value exists. For example: if (!document.getElementById('myStyle').style.opacity) alert('Aw oh!');This code always displays the alert, in Safari and Firefox. I suppose I'm missing some fundamental concept here, I'm just not sure what it is or what I can do to access the 'opacity' property.Thanks for the response!
A: 

this link help

http://www.quirksmode.org/js/opacity.html

function setOpacity(value) {
    testObj.style.opacity = value/10;
    testObj.style.filter = 'alpha(opacity=' + value*10 + ')';
}

opacity is for Mozilla and Safari, filter for Explorer. value ranges from 0 to 10.

Haim Evgi
+5  A: 

You can get the values set through class only after their computation.

var oElm = document.getElementById ( "myStyle" );
var strValue = "";
if(document.defaultView && document.defaultView.getComputedStyle)
{
strValue = document.defaultView.getComputedStyle(oElm, null).getPropertyValue("-moz-opacity");
}
else if(oElm.currentStyle)    // For IE
{
strValue = oElm.currentStyle["opacity"];
}

alert ( strValue );
rahul
Comments rule! currentStyle handles IE's way, getComputedStyle() is supported by anyone else. document.defaultView is a way of getting the `window` that is responsible for rendering the document. By the way, `-moz-opacity` will not work anymore (since FF 2, and in Opera and WebKit it never did).
Boldewyn
... use a simple 'opacity' instead of '-moz-opacity', I should have added.
Boldewyn
But i FF opacity is not applied.
rahul
@rahul: I would reccomend reading this great article: http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291 about the general issues could rise by using this function.
Marco Demajo
+1  A: 

The problem is, that element.style.opacity only stores values, that are set inside the element's style attribute. If you want to access style values, that come from other stylesheets, take a look at quirksmode.

Cheers,

Boldewyn
ah yes, or take phoenix' approach.
Boldewyn
Ah ha! Thank you very much for the link and explanation. It's very clear now.
This, however, is not only the case for opacity, but for every other style property. Try to add 'color:red;' to your stylesheet and alert(document.getElementById('myStyle').style.color) to your event handler.
Boldewyn