Friday, April 15, 2005

Change Background with Javascript

How do I change the background of a <td> using javascript.

<script language="javascript">
function shade(idx) {
str = "sp_" + idx;
//How to shade the <td> tag with the id of str?
}
</script>

<table>
<tr>
<td id="sp_1"></td>
<td id="sp_2"></td>
<td id="sp_3"></td>
<td id="sp_4"></td>
<td id="sp_5"></td>
<td id="sp_6"></td>
</tr>
<tr>
<td colspan="6"><input type="button" onClick="shade(6)"></td>
</tr>
</table>

How do I change the background color of sp_6 since I know the id name only but now the object? How do I get the object name "sp_6"?

Any help would be very much appreciated. Thanks

Answer:
<script language="javascript">
function shade(idx) {
str = "sp_" + idx;
document.getElementById(str).style.background="xxxx";
}
</script>

0 Comments:

Post a Comment

<< Home