Jump to content

changing colors


Henky

Recommended Posts

ok, i have a simple table with 2 rows, and 3 columns.
i want that when i click on a one cell, it transforms that cell from white to yellow.
how can this be done ?

i belive it's not difficult for you people, but for me as a newbie... I've tried for 4 hours now and still can't figure it out. can anyone post the code  for me ?
Link to comment
Share on other sites

I have found what you are looking for. You can find it at http://www.pageresource.com/dhtml/jtut6.htm
but it is simple:
<TABLE width="200" border="1" cellspacing="0" cellpadding="0">
<TR>
<TD style="width:100%; background-color:lightblue"
onMouseover="this.style.backgroundColor='yellow';"
onMouseout="this.style.backgroundColor='lightblue';">
Watch me get scared!
</TD>
</TR>
</TABLE>
Link to comment
Share on other sites

here's a much more useable solution -- add this javascript to the top of your page, add the classes to your CSS, and you'll be good to go:

CSS:
[code]
td {
  background-color: #FFFFFF;
}

td.highlight {
  background-color: #FCFDD0;
}
[/code]

Javascript:
[code]
function tdClick() {
  var tdEls = document.getElementById("myTable").getElementsByTagName("TD");
  for (var i = 0; i < tdEls.length; i++) {
    tdEls[i].onclick = function() {
      this.className += " highlight";
    }
  }
}

window.onload = function() {
  tdClick();
}
[/code]

whatever the ID is of your table you want to react to this needs to be reflected in the JS line getElementByID().

good luck

one other thought... if you want it to switch back to white on the second click, update the function to this:
[code]
function tdClick() {
  var tdEls = document.getElementById("myTable").getElementsByTagName("TD");
  for (var i = 0; i < tdEls.length; i++) {
    tdEls[i].onclick = function() {
      if (/highlight/.test(this.className)) {
        this.className = this.className.replace(new RegExp(" ?highlight\\b"), "");
      } else {
        this.className += " highlight";
      }
    }
  }
}
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.