changing nested span background colors

I have a "word highlighting" page in which I have words comprised of letters within nested spans, if the user unchecks a checkbox on the page it effectively clears the background color of the corresponding spans on the page.  The demo code below works in IE but not in Firefox, Safari, or Opera, which I need it to work in all 4.  If you have any questions please ask but I think the page code explains itself so please load it into a browser (preferably IE first) to see the intended effect.  Thanks.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<style type="text/css">
  .mydiv {font-family:courier new,courier;font-size:10pt;background:#EAEAEA;width:100%;word-wrap:break-word;}
  .g00 {background-color:#F5FF80;}
  .g01 {background-color:#99FF80;}
  .g02 {background-color:#80FFFF;}
</style>
<script type="text/javascript">
<!--
function changecolor(theBox){
  var obj=document.getElementsByTagName("span");
  for (i=0;i<obj.length;i++)
    {
      if(obj[i].className=="g"+theBox.name)
      obj[i].style.background=theBox.checked?theBox.value:"";
    }
}
//-->
</script>
<title>TEST</title>
</head>
<body>
<div class="mydiv">
  <fieldset style="padding:8px;margin:5px;">
    <legend>LEGEND</legend>
    <fieldset style="width:10px;padding:6px;margin:3px;background-color:#F5FF80;display:inline;">
      <legend>g00</legend>
      <input type="checkbox" value="#F5FF80" onclick="changecolor(this);" checked name="00">
    </fieldset>&nbsp;
    <fieldset style="width:10px;padding:6px;margin:3px;background-color:#99FF80;display:inline;">
      <legend>g01</legend>
      <input type="checkbox" value="#99FF80" onclick="changecolor(this);" checked name="01">
    </fieldset>&nbsp;
    <fieldset style="width:10px;padding:6px;margin:3px;background-color:#80FFFF;display:inline;">
      <legend>g02</legend>
      <input type="checkbox" value="#80FFFF" onclick="changecolor(this);" checked name="02">
    </fieldset>&nbsp;
    <br>
    <pre>
      this is a highlighting <span class="g00">t<span class="g01">e<span class="g02">s</span></span>t</span>
      unchecking/checking the "g00","g01" or "g02" checkboxes should effectively hide/show that corresponding span's color.
      this demo page works in IE but I need it to also work on Firefox, Safari, and Opera.
    </pre>
    <br>
  </div>
</div>
</body>
</html>

Open in new window

LVL 27
ddrudikAsked:
Who is Participating?
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.

thirdCommented:
try this,
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<style type="text/css">
  .mydiv {font-family:courier new,courier;font-size:10pt;background:#EAEAEA;width:100%;word-wrap:break-word;}
  .g00 {background-color:#F5FF80;}
  .g01 {background-color:#99FF80;}
  .g02 {background-color:#80FFFF;}
</style>
<script type="text/javascript">
<!--
function changecolor(theBox){
  var obj=document.getElementsByTagName("span");
  for (i=0;i<obj.length;i++)
    {
	  var currClassName = "g" + theBox.name;
	  var currClassNameX = "g" + theBox.name + "x";
 
      if(obj[i].className==currClassName || obj[i].className==currClassNameX){
        obj[i].className = theBox.checked?currClassName:currClassNameX;
	  }
    }
}
//-->
</script>
<title>TEST</title>
</head>
<body>
<div class="mydiv">
  <fieldset style="padding:8px;margin:5px;">
    <legend>LEGEND</legend>
    <fieldset style="width:10px;padding:6px;margin:3px;background-color:#F5FF80;display:inline;">
      <legend>g00</legend>
      <input type="checkbox" value="#F5FF80" onclick="changecolor(this);" checked name="00">
    </fieldset>&nbsp;
    <fieldset style="width:10px;padding:6px;margin:3px;background-color:#99FF80;display:inline;">
      <legend>g01</legend>
      <input type="checkbox" value="#99FF80" onclick="changecolor(this);" checked name="01">
    </fieldset>&nbsp;
    <fieldset style="width:10px;padding:6px;margin:3px;background-color:#80FFFF;display:inline;">
      <legend>g02</legend>
      <input type="checkbox" value="#80FFFF" onclick="changecolor(this);" checked name="02">
    </fieldset>&nbsp;
    <br>
    <pre>
      this is a highlighting <span class="g00">t<span class="g01">e<span class="g02">s</span></span>t</span>
      unchecking/checking the "g00","g01" or "g02" checkboxes should effectively hide/show that corresponding span's color.
      this demo page works in IE but I need it to also work on Firefox, Safari, and Opera.
    </pre>
    <br>
  </div>
</div>
</body>
</html>

Open in new window

0
hieloCommented:
Slight change from what you had originally. NOTE: the checkboxes were renamed g00, g01 and g02 respectively.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<style type="text/css">
  .mydiv {font-family:courier new,courier;font-size:10pt;background:#EAEAEA;width:100%;word-wrap:break-word;}
  .g00 {background-color:#F5FF80;}
  .g01 {background-color:#99FF80;}
  .g02 {background-color:#80FFFF;}
  .clearHighlight{background-color:transparent;}
</style>
<script type="text/javascript">
<!--
 
function changecolor(theBox)
{
	var obj=document.getElementsByTagName("span");
	for(var i=0,limit=obj.length;i<limit;++i)
	{
		if( -1<obj[i].className.indexOf(theBox.name) )
		{
			if( theBox.checked )
				obj[i].style.backgroundColor=theBox.value;
			else
				obj[i].style.backgroundColor='transparent';
		}
	}
}
 
//-->
</script>
<title>TEST</title>
</head>
<body>
<div class="mydiv">
  <fieldset style="padding:8px;margin:5px;">
    <legend>LEGEND</legend>
    <fieldset style="width:10px;padding:6px;margin:3px;background-color:#F5FF80;display:inline;">
      <legend>g00</legend>
      <input type="checkbox" value="#F5FF80" onclick="changecolor(this);" checked="checked" name="g00">
    </fieldset>&nbsp;
    <fieldset style="width:10px;padding:6px;margin:3px;background-color:#99FF80;display:inline;">
      <legend>g01</legend>
      <input type="checkbox" value="#99FF80" onclick="changecolor(this);" checked="checked" name="g01">
    </fieldset>&nbsp;
    <fieldset style="width:10px;padding:6px;margin:3px;background-color:#80FFFF;display:inline;">
      <legend>g02</legend>
      <input type="checkbox" value="#80FFFF" onclick="changecolor(this);" checked="checked" name="g02">
    </fieldset>&nbsp;
    <br>
    <div>
      this is a highlighting <span class="g00">t<span class="g01">e<span class="g02">s</span></span>t</span>
      unchecking/checking the "g00","g01" or "g02" checkboxes should effectively hide/show that corresponding span's color.
      this demo page works in IE but I need it to also work on Firefox, Safari, and Opera.
    </div>
    <br>
  </div>
</div>
</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
ddrudikAuthor Commented:
Both solutions worked great in all 4 browsers.  I ended up implementing the second solution, although the class swapping of the first solution is a good example to reference as well.  Thanks.
0
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.