View Single Post
Old 02-04-2013, 12:16 PM   PM User | #1
happybanana
New Coder

 
Join Date: Nov 2012
Posts: 13
Thanks: 2
Thanked 0 Times in 0 Posts
happybanana is an unknown quantity at this point
"Traffic light" html cells with javascript issue

Hi,

I've got three html table cells setup so when I click on one of them I simulate a traffic light system (and call some ajax to post the result).

<td onclick=\"togCell('green'); makemyPOSTRequest('$myvar', '3')
<td onclick=\"togCell('amber'); makemyPOSTRequest('$myvar', '2')
<td onclick=\"togCell('red'); makemyPOSTRequest('$myvar', '1')

The javascript I'm using has worked fine, up until the point that I now have the result from the traffic light saved in a database. When my php script loads it checks the result and changes the background accordingly when the page loads.

The issue is that say the traffic light loads as red, now if I click on yellow the two cells yellow and red .. if that makes sense. How do I nuetralise the red? ... or any other color. Here's the javascript I'm using:

Code:
<script type=\"text/javascript\">
var el
function togCell(col){
if (typeof event!=='undefined')
 el=event.srcElement

 for (var i = 0; i < el.parentNode.cells.length; i++)
  el.parentNode.cells[i].style.backgroundColor=''
  el.style.backgroundColor=col
}
if (window.addEventListener)
window.addEventListener('click', function(e){el=e.target}, true)
</script>
happybanana is offline   Reply With Quote