faqts : Computers : Programming : Languages : JavaScript : Tables

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

82 of 97 people (85%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

How do I change the bgColor and the fgColor of a table cell?

Sep 10th, 2000 07:39
Martin Honnen,


Only NN6 and IE4+ can change both the background and the foreground 
(text) color of a table cell, NN4 can only change the background color; 
here is a code example:
<HTML>
<HEAD>
<STYLE>
#aCell {
  position: relative;
}
</STYLE>
<SCRIPT>
function setFgColor (color) {
  if (document.all)
    document.all.aCell.style.color = color;
  else if (document.getElementById)
    document.getElementById('aCell').style.color = color;
}
function setBgColor (color) {
  if (document.all)
    document.all.aCell.bgColor = color;
  else if (document.getElementById)
    document.getElementById('aCell').bgColor = color;
  else if (document.layers)
    document.aCell.bgColor = color;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="formName">
<TABLE BORDER="1">
<TR>
<TD>
fgColor:
<INPUT TYPE="text" NAME="fgColor" SIZE="10" VALUE="white">
<INPUT TYPE="button" VALUE="set"
       ONCLICK="setFgColor(this.form.fgColor.value)">
<BR>
bgColor:
<INPUT TYPE="text" NAME="bgColor" SIZE="10" VALUE="orange">
<INPUT TYPE="button" VALUE="set"
       ONCLICK="setBgColor(this.form.bgColor.value)"
>
</TD>
<TD ID="aCell">
Kibology for all. 
<BR>
All for Kibology.
</TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>