Toggle cell color

I would like to be able to toggle between foreground and background colors of a cell in a table onClick.

The code below does not seem to work to change the background color of a cell.  I would appreciate it if someone could debug it for me or suggest some other code to accomplish this task.

Thank you.

<html>

<head>
<Script Language=JAVASCRIPT TYPE="TEXT/JAVASCRIPT">
<!--Hide script from old browsers
function CellColor(cellname) {
     if(document.all){
      cell = eval("document.all (" + cellname + ").style")
          if (cell.background == "'red'") {
              cell.background = "'white'"
     }
          else {
               cell.background = "'red'"
          }
          return false
          else {
          }
         
//end hiding script-->
</script>

</head>


<body>

<table border=1>
<tr>          
<td id="changeme" background="white"><span
 onClick="return CellColor('changeme')">A</span></td>
</tr>
</table>

</body>

</html>
gcasselAsked:
Who is Participating?
 
thirdCommented:


<html>

<head>
<Script Language=JAVASCRIPT TYPE="TEXT/JAVASCRIPT">
<!--Hide script from old browsers
function CellColor(cellname) {
  if(document.all){
    var cell = eval("document.all." + cellname);
    if (cell.style.backgroundColor == 'red') {
      cell.style.backgroundColor = 'white';
      cell.style.color = 'red';
    }
    else if (cell.style.backgroundColor == 'white') {    
      cell.style.backgroundColor = 'red';
      cell.style.color = 'white';
    }
  }
}        
//end hiding script-->
</script>
</head>
<body>
<table border=1>
<tr>          
<td id="changeme" style="background-color:white;color:red" onClick="CellColor('changeme')">A</td>
</tr>
</table>
</body>
</html>
0
 
jhullyCommented:
I'd like to try
next:
(sorry may be it will be good only for IE)
it will also give you square form of table to colorize it
<head>
<script language="JScript">
<!--Hide script from old browsers
function CellColor(i,j)
{
    var cell = tblName.rows.item(i).cells.item(j);
    var col = cell.bgColor == "red" ? "white" : "red";
    cell.bgColor = col;
}        
//end hiding script-->
</script>

</head>


<body>

<table id="tblName">
<tr>          
     <td onclick="CellColor(0,0)"></td>
</tr>
</table>
</body>

</html>
0
 
thirdCommented:
could you please justify the grade B??
0
 
Computer101Commented:
A request has been made to change the grade.  If no input concerning the grade, I will change it.

The guidelines here.

How Do I Know What Grade to Give?
Although we use an A-D scale here at Experts Exchange, it works differently than, say, school grades. If one or more Experts' proposals are accepted as answers, they should usually be given an A or B grade, since they have taken the time to provide you with a working solution. If a possible solution is incomplete - ask for clarification or details before accepting the answer and grading it. People should not be given lower grades because of incorrect grammar or because you just accepted their answer or comment to close the question. Keep in mind, your question and any follow-up comments should be focused so that there can be a specific answer. The following is a good guideline to follow when grading:

A: The Expert(s) either provided you with a thorough answer or they provided you with a link to information that thoroughly answered your question. An "A" can also be given to any answer that you found informative or enlightening beyond the direct question that you asked.

B: The Expert(s) provided an acceptable solution, or a link to an acceptable solution, that you were able to use, although you may have needed a bit more information to complete the task.

C: Because Experts' reliability are often judged by their grading records, many Experts would like the opportunity to clarify if you have questions about their solutions. If you have given the Expert(s) ample time to respond to your clarification posts and you have responded to each of their posts providing requested information; or if the answers, after clarification, lack finality or do not completely address the issue presented, then a "C" grade is an option. You also have the option here of just asking Community Support to delete the question.

Remember, the Expert helping you today is probably going to be helping you next time you post a question. Give them a fair chance to earn an 'Excellent!' grade and they'll provide you with some amazing support


Computer101
E-E Moderator
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.