How can I use onclick to change a table cell background color

Discussion in 'Javascript' started by Michael, May 14, 2004.

  1. Michael

    Michael Guest

    How can I use onclick to change a table cell background color. I know
    I can use this.className=?

    to change the classname. But then I would have to put the code into
    every single table cell. I read once about using a collection method.

    Lets say a table has 100 TD Cells with a white background. I want to
    click on just 1 and then have it change to a Yellow background. Then
    when I click on a different Cell have the old one change back to white
    and the new Cell change to Yellow. etc.
     
    Michael, May 14, 2004
    #1
    1. Advertisements

  2. Michael

    Woody Guest

    function BgnDay_onclick() {
    var IdString = ""
    var TempDay = 0
    var GoodPick = true

    if (DateSet == false) {
    IdString = window.event.srcElement.id;
    if (IdString.length < 4) {
    IdString = window.event.srcElement.parentElement.id;
    if (IdString.length < 4)
    IdString = window.event.srcElement.parentElement.parentElement.id; }
    if (document.all(IdString).innerText !== "") {
    TempDay = Math.abs(CellToDayNumber(IdString));
    if (document.all(IdString).style.backgroundColor !== "red") {
    if (StartCell.length > 0) {
    BackColor(StartCell);
    StartDay = TempDay; }
    RedItOut(IdString);
    StartDay = Math.abs(CellToDayNumber(IdString));
    StartCell = IdString;}
    else
    { BackColor(IdString);
    StartCell = "";
    StartDay = 0 }}}
    DisplayDate();

    butchered from some code i wrote for a project

    Woody
    any sugestion or comment made by me should be examined first for
    validity and appropriateness before assuming i have any idea at all
    what the heck i am talking about. I am not responsible for anything you
    may see with my name attached to it, i think.
     
    Woody, May 15, 2004
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.