Welcome to WebmasterWorld Guest from 54.146.221.231

Forum Moderators: open

Message Too Old, No Replies

setting CSS via JS

     
10:10 pm on Dec 4, 2011 (gmt 0)

Preferred Member

10+ Year Member

joined:Nov 10, 2005
posts: 387
votes: 0


I can't get the following portion of my javascript to set CSS opacity:

<script language="JavaScript">
...
var trans = 0.5;
elementid.opacity = trans;
</script>

This doesn't work either:

document.getElementById("elementid").opacity = trans;
11:21 pm on Dec 4, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 0


document.getElementById("elementid").style.opacity = trans;

You will also need to set the filter property if you intend to support IE8 and below...
document.getElementById("elementid").style.filter = 'alpha(opacity=' + trans*100 + ')';
12:49 pm on Dec 5, 2011 (gmt 0)

Preferred Member

10+ Year Member

joined:Nov 10, 2005
posts: 387
votes: 0


Thanks penders, that was it.

Yes, I have the IE8 code too; that came with the script and was working fine, but I couldn't convert it for other browsers. It's in a slightly different format:

elementid.filters( "alpha" ).opacity = 50;