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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change background colour of table cell?

Status
Not open for further replies.

cyberprof

Programmer
Jun 10, 2003
229
0
0
GB
Is it possible to dynamically change the background of a table cell when you click on a hyperlink within that cell, without refreshing the page?

Cheers

J
 
Code:
<table style="background:red;" id="myTable">
	<tr><td><a href="javascript: document.getElementById('myTable').style.backgroundColor='blue'">Change</a></td></tr>
</table>

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook

zen.gif

 
When I click on the hyperlink 'Change' the word 'Blue' appears, but the background colour doesn't change
 
While it doesn't show the cursor to indicate a link, the following works:

<table style="background:red;" id="myTable">
<tr><td onclick="document.getElementById('myTable').style.backgroundColor='blue'">Change</td></tr>
</table>

Lee
 
Code:
<table style="background:red;" id="myTable">
    <tr>
    	<td><a href="#" onClick="document.getElementById('myTable').style.backgroundColor='blue'">Change</a></td>
    </tr>
</table>

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook

zen.gif

 
If you want many rows and cells in a table, but only one cell to change when it's clicked on, the following would be more flexible:

<table>
<tr><td style="background:red;" onclick="this.style.backgroundColor='blue'">Change 1</td>
<td style="background:red;" onclick="this.style.backgroundColor='blue'">Change 2</td></tr>
</table>

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top