Toggle row color when Checkbox is checked/unchecked
Hi - I have been fighting with this for a couple of days now. I have a dynamic table that is built via a query which has a checkbox at the beginning of the row. When a user clicks or checks a checkbox I need to have the row highlight to a different color and then if the user unchecks the checkbox I need to have the row go back to the original color. Below is my code at this point. I can get the row to highlight, but I cannot get the row to un-highlight when I check it again.
Thanks in advance for the help!
<script type="text/javascript"><!--
function gregtest2()
{
var message = '';
for (var i = 0; i <= 249; i++) {
testchecked = document.form.elements[i].checked;
if (testchecked == true) {
try{HighLightTR('white','cc3333');}catch(e){;}
}
if (testchecked == false) {
try{HighLightTR('gold','cc3333');}catch(e){;}
}
// document.form.price.value = ('$' + document.form.price.value);
// $price = document.form.price.value;
}
}
</script>
<script>
var preEl ;
var orgBColor;
var orgTColor;
function HighLightTR(backColor,textColor){
// if(typeof(preEl)!='undefined') {
// preEl.bgColor=orgBColor;
// try{ChangeTextColor(preEl,orgTColor);}catch(e){;}
// }
var el = event.srcElement;
el = el.parentElement;
orgBColor = el.bgColor;
orgTColor = el.style.color;
el.bgColor=backColor;
“The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
—Tim Berners-Lee, W3C Director and inventor of the World Wide Web
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"><html><head><title>Highlighting rows</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><style type="text/css" media="screen"><!--
/* Background color of unhighlighted row */
.rowA td {
background-color: green;
}
/* Background color of row when highlighted */
.rowB td {
background-color: gold;
}
--></style><script type="text/javascript"><!-- begin hiding
window.onload = function() {
if (document.getElementsByTagName) {
var inputs = document.getElementsByTagName('input');
for (var i=0, end=inputs.length; i < end; i++) {
if (inputs[i].className === 'toggleCheck') {
inputs[i].onclick = function() {
var TRnode = this.parentNode;
while (TRnode.nodeName !== 'TR') {
TRnode = TRnode.parentNode;
}
TRnode.className = TRnode.className === 'rowA' ? 'rowB' : 'rowA';
};
}
}
}
};
// end hiding --></script></head><body><table class="tableoptions"><thead><tr><th colspan="2" id="th_options">Options</th><th id="th_cost">Cost</th></tr></thead><tbody><?php for ($i = 1; $i <= 250; $i++):?><?php
$options = $i + 9;
$cost = $i + 259;
?><?php if ($a_row[$options] != ''):?><tr class="rowA"><td><input type="checkbox" class="toggleCheck" value="<?php $a_row[$options]|$a_row[$cost]; ?>" name="chkprice[]"><input type="hidden" name="cost1" readonly value="<?php echo $a_row[$cost] ?>"></td><td class="options" headers="th_options"><?php echo $a_row[$options]; ?></td><td align="right" width="60" headers="th_cost">
$<?php echo $a_row[$cost]; ?>.00
</td></tr><?php endif;?><?php endforeach;?></tbody></table></body></html>
This method keeps you HTML a little cleaner if you create the onclick even handlers in the window's onload event. I also prettied up the PHP and fleshed out the markup for the table a little. You had some extra HTML tags you didn't need and was missing some tags that increase the accessibility of the table's data.
Bookmarks