Jump to content


Photo

changing colors


  • Please log in to reply
5 replies to this topic

#1 Henky

Henky
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 27 August 2006 - 12:58 AM

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 ?

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 27 August 2006 - 01:37 AM

It certainly isn't php.  Javascript would be able to do it.

Moving your post to that forum.
Legend has it that reading the manual never killed anyone.
My site

#3 Henky

Henky
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 27 August 2006 - 02:47 AM

anyone ? :(

#4 jaronblake

jaronblake
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 29 August 2006 - 04:56 PM

you can create buttons that can change the color of the cell but as for clicking on the cell itself i dont think thats possibe but i could be wrong.

#5 jaronblake

jaronblake
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 29 August 2006 - 07:14 PM

I have found what you are looking for. You can find it at http://www.pageresou...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>

#6 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 29 August 2006 - 07:23 PM

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:
td {
  background-color: #FFFFFF;
}

td.highlight {
  background-color: #FCFDD0;
}

Javascript:
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();
}

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:
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";
      }
    }
  }
}

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users