www.webdeveloper.com
Results 1 to 5 of 5

Thread: menu changes color of table cell

  1. #1
    Join Date
    Jul 2010
    Posts
    2

    menu changes color of table cell

    Just curious if there is a way to control a table cell color based on a menu selection.

    I have several colors like Red, Navy, Black, Green, Gold and would like to show a swatch of this selected color beside the menu.

  2. #2
    Join Date
    Jul 2010
    Location
    United States
    Posts
    9
    Code:
    var cells = document.getElementsByTagName("td"); //Get all table cells just once.
    
    //Cycle through array and change background color of each cell:
      for(var i=0; i<cells.length; i++){
        cells[i].style["background-color"] = "COLOR GOES HERE";
      }

  3. #3
    Join Date
    Jul 2010
    Location
    United States
    Posts
    9
    Of course, to make this as re-usable as possible, put the color-changing part into a function that accepts a parameter, that way changing colors is as simple as
    Code:
    changeColor("gold");

  4. #4
    Join Date
    Jul 2010
    Posts
    2
    Hmm, thanks for the reply. I don't see anything in there that changes the color based on a menu selection. I don't want to do all table cells, just one. I can put an ID on this one cell right? Then use getelementbyid instead of tag name.

  5. #5
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by jfkansas View Post
    I can put an ID on this one cell right? Then use getelementbyid instead of tag name.
    Yes. Or you may still use the tag name, but set the index of that tag within its collection.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles