The opacity property is set to css:

#something { opacity: .5; } 

HTML:

 <body id="something"> 

How do i get the value of this property in javasciprt? We need an exact answer from someone who knows for sure.

  • There is an article on the webew about this. Explore her. =) - ling

4 answers 4

 function getStyle(el,styleProp) { var x = document.getElementById(el); if (x.currentStyle) var y = x.currentStyle[styleProp]; else if (window.getComputedStyle) var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp); return y; } alert(getStyle("something", "opacity")); 

    Let's replace the word f ** k with di

     document.getElementById('di').style.opacity 
    • Why then alert of your record displays undefined ?? - Jet
    • So it is not installed programmatically. - Andrei Arshinov
    • And if you manually set the value does not display? - Jet
    • At least due, due to different browsers, different values ​​are returned. - Andrei Arshinov
    • In IE, renders undefined; in firefox, displays an empty value. - Jet
     $("#something").css("opacity"); 

    If you are interested in looking at the most complete code snippet, then like this:

     <script type="text/javascript" src="http://yandex.st/jquery/1.6.2/jquery.min.js"> </script> <script> $(document).ready(function() { alert("opacity: " + $("#something").css("opacity")); }); </script> <style> #something { opacity: 0.45; } </style> <body id="something"> </body> 
    • Sorry, but it is very important for me to do it on pure JS - Jet
     $(document.body).css("opacity"); 

    The jQuery library is used.

    Try to use markup and do not use informal language.

    How to use markup here .