SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jul 2001
    Location
    Vancouver, WA
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need to change TD bgColor with onload event

    I know this is a very simple problem, but for the life of me I can't get it to work.

    I have a navigation menu made up of TD cells that are blue by default. Each TD cell contains a link to a section of my site. I need the TD cell containing the current page's title to change to white when the page loads via an onLoad trigger in the body tag. Below you can see a simplified version of what I'm doing. Either one of the calls in the function aren't working because 'document.all.cell.style' is null or not an object.

    I need to know what I need to do to make this simple call work. I know it's something ridiculously basic, but I could use some help. Thanks for any input you can offer.

    Code:
    <html>
    <head>
    
    <script language="JavaScript">
    <!--
    var cell="td1"  //Specifies which cell to highlight
    
    function loadtab(cell) {
      document.all.cell.style.backgroundColor="#ffffff";
      //document.all.cell.bgColor="#ffffff"; (Alternate method)
    }
    //-->
    </script>
    
    </head>
    <body onLoad="loadtab(cell)">
    
    <TABLE width="100%" bgColor="blue">
    	<TR>
    		<TD ID="td1">Cell 1</TD>
    		<TD ID="td2">Cell 2</TD>
    		<TD ID="td3">Cell 3</TD>
    		<TD ID="td4">Cell 4</TD>
    		<TD ID="td5">Cell 5</TD>
    	</TR>
    </TABLE>
    
    </body>
    </html>

  2. #2
    SitePoint Enthusiast
    Join Date
    Nov 2002
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    to dynamically refer to an element you need to use this syntax...

    document.all[cell].style.backgroundColor

    I would also suggest that you use getElementById instead of document.all. document.all only works in IE where as getElementById works in both IE5+ and NS6+. The code would look like this.

    document.getElementById(cell).style.backgroundColor
    travis

  3. #3
    SitePoint Member
    Join Date
    Jul 2001
    Location
    Vancouver, WA
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile

    Thank you podlob! I knew it was something simple


Bookmarks

Posting Permissions

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