JavaScript: set -moz-opacity for old Gecko

Firefox 0.8 does not support opacity.  Instead it uses:
  -moz-opacity

How can I set -moz-opacity with JavaScript?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Box</title>
<style type="text/css">
</style>
</head>
<body>

<h1 id="zzz">Hello World</h1>

<script type="text/javascript">
	var obj = document.getElementById('zzz');
	 if(obj) {
	 obj.style.background = 'red';
	 obj.style.-moz-opacity  = '.7';
	 }
</script>

</body>
</html>

Open in new window

LVL 16
hankknightAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Adoryc666Commented:
This should work.
<script type="text/javascript">
	var obj = document.getElementById('zzz');
	 if(obj) {
	 obj.style.background = 'red';
	 obj.style.MozOpacity  = '.7';
	 }
</script>

Open in new window

0
hieloCommented:
my suggestion would be to put all the css on a single class definition and instead of assigning individual css rules per element, give your dynamic elements the class:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Box</title>
<style type="text/css">
.dynamicElement{
  -moz-opacity  = '.7';
  background-color:red;
}
</style>
</head>
<body>

<h1 id="zzz">Hello World</h1>

<script type="text/javascript">
        var obj = document.getElementById('zzz');
         if(obj) {
         obj.className+=' dynamicElement';
         }
</script>

</body>
</html>

Open in new window

0
hankknightAuthor Commented:
Thank you both.

hielo, I understand your suggestion however the reason I did not do it that way was because -moz-opacity is NOT valid CSS.  It does not validate and it produces warnings.  I am trying to find a way to make this work on old versions of Firefox without producing warnings with invalid code.  Is there a way to follow your suggestion using valid code?
0
hieloCommented:
try the following:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Box</title>

</head>
<body>

<h1 id="zzz">Hello World</h1>

<script type="text/javascript">
window.onload=function(){
	insertCSS();
}

function insertCSS(){
	var rules='body{color:blue;} .dynamicElement{-moz-opacity:.7;background-color:red;}';
	var h=document.getElementsByTagName('head')[0];
	var s=document.createElement('style');
	s.setAttribute("type", "text/css");
	if(s.styleSheet)// IE
	{
		s.styleSheet.cssText = rules;
	}
	else
	{
		var t = document.createTextNode(rules);
		s.appendChild(t);
	}
	h.appendChild(s);
};

        var obj = document.getElementById('zzz');
         if(obj) {
         		obj.className+=' dynamicElement';
         }

</script>

</body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.