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?
 
hieloConnect With a Mentor Commented:
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
 
thirdConnect With a Mentor Commented:
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
 
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
All Courses

From novice to tech pro — start learning today.