Dynamically change bgColor of table cell?

Discussion in 'Javascript' started by kj, Feb 11, 2004.

  1. kj

    kj Guest

    Is there a way to dynamically (e.g. upon onClick) change the bgColor
    attribute of a table cell? I tried cell.setAttribute("bgColor",
    someColor), where someColor was a string of the form "#RRGGBB",
    but nothing happened.

    Also, how can I set the mouse cursor to be an arrow when it hovers
    over these table cells? (It's an insertion bar now.)

    Thanks!

    kj
    kj, Feb 11, 2004
    #1
    1. Advertising

  2. On Wed, 11 Feb 2004 22:46:35 +0000 (UTC), kj <> wrote:

    > Is there a way to dynamically (e.g. upon onClick) change the bgColor
    > attribute of a table cell? I tried cell.setAttribute("bgColor",
    > someColor), where someColor was a string of the form "#RRGGBB",
    > but nothing happened.


    Try

    cell.style.backgroundColor = '#rrggbb';

    > Also, how can I set the mouse cursor to be an arrow when it hovers
    > over these table cells? (It's an insertion bar now.)


    You can use CSS to do this.

    <style type="text/css">
    td {
    cursor: default;
    }
    </style>

    This will make the cursor display as the default shape (an arrow, usually)
    over all table cells, assuming the cursor property is supported. If you
    want to be more selective, change the selector to include a class.

    Mike

    --
    Michael Winter
    d (replace ".invalid" with ".uk" to reply)
    Michael Winter, Feb 12, 2004
    #2
    1. Advertising

  3. kj

    McKirahan Guest

    "kj" <> wrote in message
    news:c0ebcb$494$...
    >
    >
    >
    > Is there a way to dynamically (e.g. upon onClick) change the bgColor
    > attribute of a table cell? I tried cell.setAttribute("bgColor",
    > someColor), where someColor was a string of the form "#RRGGBB",
    > but nothing happened.
    >
    > Also, how can I set the mouse cursor to be an arrow when it hovers
    > over these table cells? (It's an insertion bar now.)
    >
    > Thanks!
    >
    > kj



    Will this help?

    <html>
    <head>
    <title>cellfunc.htm</title>
    <script type="text/javascript">
    function doit(that,what) {
    if (what != "") {
    that.style.cursor = what;
    } else {
    that.style.background = "yellow";
    }
    }
    </script>
    </head>
    <body>
    <table border="1" width="200" height="200">
    <tr valign="top">
    <td id="cell1"
    onmouseover="doit(this,'hand')"
    onmouseout="doit(this,'auto')"
    onclick="doit(this,'')">
    <br> blah, blah, blah, blah, blah...
    </td>
    </tr>
    </table>
    </body>
    </html>
    McKirahan, Feb 12, 2004
    #3
  4. kj

    McKirahan Guest

    "kj" <> wrote in message
    news:c0ebcb$494$...
    >
    >
    >
    > Is there a way to dynamically (e.g. upon onClick) change the bgColor
    > attribute of a table cell? I tried cell.setAttribute("bgColor",
    > someColor), where someColor was a string of the form "#RRGGBB",
    > but nothing happened.
    >
    > Also, how can I set the mouse cursor to be an arrow when it hovers
    > over these table cells? (It's an insertion bar now.)
    >
    > Thanks!
    >
    > kj



    Cool!

    One problem: I get #666 when I should get "black".


    P.S. I modified one line to display the color on the status bar:

    <div id="g1" onmouseout="window.status=''"
    onmouseover="window.status=window.event.srcElement.strokeColor"
    onclick="alert(window.event.srcElement.strokeColor)"></div>

    This lets the user see the color without having to click.


    Where did you learn about this vml stuff?
    McKirahan, Feb 12, 2004
    #4
  5. kj

    kj Guest

    Thanks. That worked great.

    One tiny remaining problem is that even though the cursor is no
    longer an text insertion cursor, it can select text. If the mouse
    moves slightly when one clicks on a cell, some text gets selected.
    Is there any way to prevent this from happening?

    Thanks!

    kj
    kj, Feb 12, 2004
    #5
  6. kj

    McKirahan Guest

    "McKirahan" <> wrote in message
    news:NhMWb.13527$jk2.45500@attbi_s53...
    > Cool!
    >
    > One problem: I get #666 when I should get "black".


    [snip]

    Sorry for the above message; it was for another thread...
    McKirahan, Feb 12, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sam
    Replies:
    2
    Views:
    1,581
    Bobby Ryzhy
    Jul 8, 2004
  2. Mike

    Table cell bgcolor size ?

    Mike, Oct 29, 2003, in forum: HTML
    Replies:
    2
    Views:
    724
    Jukka K. Korpela
    Oct 29, 2003
  3. tonicvodka

    Swapping bgcolor in table cell

    tonicvodka, Aug 8, 2005, in forum: HTML
    Replies:
    5
    Views:
    410
  4. A.S.
    Replies:
    4
    Views:
    166
    Richard Cornford
    Jul 28, 2003
  5. coolsti
    Replies:
    5
    Views:
    202
    coolsti
    Jun 2, 2005
Loading...

Share This Page