Search code examples
javascriptcssopacity

How do I get the opacity of an element using Javascript?


If I have:

#em {
  opacity:0.5;
}

How do I get #em's opacity using javascript? :D

I've got troubles with the following (it returns nothing):

return document.getElementById("em").style.opacity;

Solution

  • Setting a CSS value in a stylesheet is not the same as setting it through the style property. You need to look at the getComputedStyle method to obtain this (and also currentStyle for older IE).