Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
like image 214
John Avatar asked Jul 06 '12 15:07

John


1 Answers

var em = document.getElementById("em");
var  temp = window.getComputedStyle(em).getPropertyValue("opacity");

Now, the variable temp will have the value of opacity of "em".

like image 183
Sajib Biswas Avatar answered Sep 19 '22 16:09

Sajib Biswas