Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change cell color after multiplication 1

Status
Not open for further replies.

hedidit

Technical User
Jul 21, 2005
142
GB
Hi I have two select menus on my page, I want a cell color to change depending upon what values are chosen. The javascript code was kindly supplied by someone on this forum and looks like this:

Code:
<script>
function changeCol(){
  if(document.getElementById('currlikli').value != "" && document.getElementById('currSev').value != ""){
     var colval = document.getElementById('currlikli').value * document.getElementById('currSev').value;
     if(colval =0){colval = "#FFFFFF";}
     if(colval >6){colval = "#00FF00";}
     if(colval <=10){colval = "#FFFF33";}
     if(colval <=15){colval = "#FF8800";}
     if(colval >=16){colval = "#FF0000";}
     document.getElementById('cell_rating').style.backgroundColor = colval;
   }
}
</script>

The two select menus are populated from a database using ASP, the following shows what they look like and the values they contain:

Code:
<Select id="currlikli" name="currlikli" onchange="changeCol();">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</Select>

<Select id="currSev" name="currSev" onchange="changeCol();">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</Select>

The table cell i want to change color looks like this:

Code:
<td name="cell_rating" id="cell_rating" width="15%">

My problem is that for some reason the cell changes color one the first update to the select menus but doesn't change after that... can anyone help me out?

Cheers
 
The problem is that colval gets redefined on the first if statment and set to 0. You need to use double equals to check for equality
Code:
<script>
function changeCol(){
  if(document.getElementById('currlikli').value != "" && document.getElementById('currSev').value != ""){
     var colval = document.getElementById('currlikli').value * document.getElementById('currSev').value;
     if(colval [!]==[/!] 0){colval = "#FFFFFF";}
     if(colval >6){colval = "#00FF00";}
     if(colval <=10){colval = "#FFFF33";}
     if(colval <=15){colval = "#FF8800";}
     if(colval >=16){colval = "#FF0000";}
     document.getElementById('cell_rating').style.backgroundColor = colval;
   }
}
</script>

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
Hey thanks for the quick response kaht, do you know whether there's anyway to change the value of the text in the cell as well? For example if the cell is color #00ff00 i'd like the cell to show text saying 'Low'...

Thanks again
 
assuming that the cell you want to change also has an id of "cell_rating" and all that is inside the cell is text (if you have any other controls or anything else in the cell they will be overwritten with this solution) then this should work:
Code:
document.getElementById('cell_rating').innerHTML = "some new message"

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
Haha thats fantastic!

Thanks a lot
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top