Forum Moderators: open

Message Too Old, No Replies

Change color of a cell onClick...

Without using an id...

         

mipapage

10:58 pm on Dec 20, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have the following code:

<body><table><tr> 
<td id="one" style="background:#fff">
Si<input type="radio" name="occupwk1" value="1"
onclick="document.getElementById('one').style.background='red'" />
No<input type="radio" name="occupwk1" value="0"
onclick="document.getElementById('one').style.background='green'" />
</td></tr></table></body></html>

This does what I want it to, however I have up to 16 boxes like that in the table, and I was wondering if there was any way to do this without having to specify nn ID for each box(td).

Is there a way with JS where I can simply adjust a property of the parent element?

Birdman

1:34 pm on Dec 21, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello mipapage,

I got this example working in Opera and IE. I tried to make it into a simpler function() with no success :(


<html>
<body>
<table>
<tr>
<td style="background:#fff">
Si<input type="radio" name="occupwk1" value="1"
onclick="this.parentNode.style.background='red'" />
No<input type="radio" name="occupwk1" value="0"
onclick="this.parentNode.style.background='green'" />
</td>
</tr>
</table>
</body>
</html>

mipapage

2:29 pm on Dec 21, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey birdman!

Beautifully done, and that simple eh? ;-]

Well, it's certainly simpler than having to name off a bunch of id's, thanks a lot!

- mipapage

btw - it work in mozilla too.