Results 1 to 3 of 3
  1. #1
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Nottingham, UK, Nottinghamshire, England
    Posts
    190
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript - altering table element background col

    I'm very new to Javascript (much more at home with VB), and I'm currently writing a bingo app for our divisional intranet. I'm trying to write a routine that will alter the background colour for each individual cell in a table depending on certain conditions, so that the colour can vary from cell to cell. Determining the colour is no problem, but when it comes to altering the cell background colour accordingly, I'm stuck with the very longwinded code as follows:
    switch(i)
    {
    case 1:
    board1.style.backgroundColor = vBoardColour;
    break
    case 2:
    board2.style.backgroundColor = vBoardColour;
    break
    ...... and so on down to 'case 90' where 'board1' and the like are the ids for each individual <td> element in the table.

    Is there any way that I can incorprate the current value of i in the javascript so that I only have to code one line, say:
    'board' + i.style.backgroundColor = vBoardColour; // yes, I KNOW this doesn't work!!

    I'm thinking that there must be a way to code this, but I'm not seeing it yet! Can anyone help? Thanks.

  2. #2
    Platinum Lounger
    Join Date
    Jan 2001
    Location
    Quedgeley, Gloucester, England
    Posts
    5,333
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Javascript - altering table element background

    Am I missing something, for surely you could use a FOR loop...?
    <font face="Script MT Bold"><font color=blue><big><big>John</big></big></font color=blue></font face=script>

    Ita, esto, quidcumque...

  3. #3
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Nottingham, UK, Nottinghamshire, England
    Posts
    190
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Javascript - altering table element background

    Thanks, John. You're right of course, and I am using a FOR loop, just not showing it! The code shown is at the end of the loop. My problem was that, while iterating around on values of i, I couldn't think how to alter the background colour of a 'td' element with id of 'board*' where * is the current value of i. I've just (within the last 5 minutes!) found what to do, namely:
    document.getElementById("board" + i).style.backgroundColor=vBoardColour;

    This works just fine! I've been playing for a long time trying the SetAttribute method (eg document.getElementById("board" + i).SetAttribute("bgcolor" , vBoardColour)[img]/forums/images/smilies/wink.gif[/img] but to no avail; nothing happened. Any idea why this is?

    Anyway, I've solved my problem (just a lack of knowledge and experience, is all) but thanks for the reply and the consideration. Much appreciated.

Posting Permissions

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