Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Td background

  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Location
    TX
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Td background

    Hi,

    I have tried to figure this out but haven't had any luck..

    I need a td (id="td1") to have a bgcolor of black when a variable is equal to 1.
    So:

    var x = equation that is equal to 1;
    if (x == 1) {
    Code to make td have a black bg;
    } else { Don't change anything.}

    I am stuck because all the resources that I could find are talking about td:hover CSS.

    - Waffles
    Last edited by Waffles32; 01-18-2012 at 10:41 PM. Reason: Typo

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,762
    Thanks
    55
    Thanked 517 Times in 514 Posts
    Code:
    if (x == 1) {
    document.getElementById("td1").style.backgroundColor="#000000"
    }

  • Users who have thanked xelawho for this post:

    Waffles32 (01-18-2012)

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Code:
    <table>
    <tr>
    <td id = "td1">Some Text Here
    </td>
    </tr>
    </table>
    
    <script type = "text/javascript">
    
    var x = 1;
    if (x==1) {
    document.getElementById("td1").style.backgroundColor = "black"
    }
    
    </script>
    If your styling is more complicated than that, you should define a css class and change the className as appropriate.

    Quizmaster: What is the English translation of the French word voiture?
    Contestant: A tree.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    Waffles32 (01-18-2012)

  • #4
    New to the CF scene
    Join Date
    Jan 2012
    Location
    TX
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you both very much!
    My page is working smoothly now and it looks great!

    - Waffles


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •