SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast devhen's Avatar
    Join Date
    Jul 2002
    Location
    Salt Lake City, UT
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Changing table background colors - doesn't work in IE6

    I've been using this code to change the backgroung color of table cells (when the mouse moves over that cell):

    if (document.getElementById)
    document.getElementById(theCell).style.backgroundColor = theColor;


    But ever since I installed IE6, I get JavaScript errors. Is there a different way to do this that IE6 will accept?

  2. #2
    SitePoint Wizard Bill Posters's Avatar
    Join Date
    Dec 2001
    Location
    UK
    Posts
    1,523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What are the error messages you are getting?

    You show two (presumed) variables, so without seeing the script in context or the error messages it is hard to say which part of the script is causing it to fail.

    Do you have a url for the problem page where we can see it in context?
    New Plastic Arts: Visual Communication | DesignateOnline

    Mate went to NY and all he got me was this lousy signature

  3. #3
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Originally posted by Bill Posters
    You show two (presumed) variables, so without seeing the script in context or the error messages it is hard to say which part of the script is causing it to fail.
    That's a very valid point, try putting this code in the onMouseOver event of an image or anything that supports this method to test that JS is working correctly and to help pinpoint the problem.
    Code:
    document.getElementById('cell_id_here').style.backgroundColor='black'

  4. #4
    SitePoint Enthusiast devhen's Avatar
    Join Date
    Jul 2002
    Location
    Salt Lake City, UT
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is the function that is causing the error:

    function doMouseOver(theCell,theColor) {
    if (document.all)
    document.all.theCell.style.backgroundColor = theColor;
    else if (document.getElementById)
    document.getElementById(theCell).style.backgroundColor = theColor;
    }

    The document.all section is for NS/Mozilla and document.getElementById is for IE. But now IE6 is returning 'true' for document.all and trying to run the NS code. I discovered this by looking at the error, which mentions the NS code:

    Error: 'document.all.theCell.style' is null or not an object

    So, what conditional should I be using to check for NS?
    Last edited by devhen; Feb 6, 2003 at 15:13.

  5. #5
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    as far as I knew document.all was IE-only. (Could be wrong)
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  6. #6
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yup. alert(document.all) returns [object] for IE, and undefined for Mozilla.

    however I don't see a real problem with that code right now either :S
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  7. #7
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you do have a cell in the document with the ID you specified don't you?

    (Does it work in Mozilla for you?)

    Code:
    function changeTD(obj_name, color)
    {
    	if (document.getElementById) {
    		element = document.getElementById(obj_name); }
    	else if (document.all) {
    		element = document.all.obj_name; }
    	element.style.backgroundColor = color;
    }
    that works for me here.
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  8. #8
    SitePoint Enthusiast devhen's Avatar
    Join Date
    Jul 2002
    Location
    Salt Lake City, UT
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks, cyborg. your code worked for me. but i still don't know why mine wouldn't work. all I know is that it worked until I installed IE6... ???

  9. #9
    SitePoint Enthusiast
    Join Date
    Nov 2002
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    document.all is for IE 4+ and up (Not Mozilla).

    IE 5+ can do both document.all and document.getElementByID

    Mozilla, Netscape 6+ only does document.getElementByID

    Netscape 4 does document.layers (but who cares.)

    --------------------

    The problem with your function was that you didn't have brackets in your document.all statement.

    this:
    document.all.theCell.style.backgroundColor
    should be:
    document.all[theCell].style.backgroundColor

    if you don't put brackets around theCell, the script will look for an element called "theCell", and it won't use the variable passed into the function.

    fixed code

    Code:
    function doMouseOver(theCell,theColor){
      if(document.all){
        document.all[theCell].style.backgroundColor = theColor;
      }else if(document.getElementById){
        document.getElementById(theCell).style.backgroundColor = theColor;
      }
    }
    travis

  10. #10
    SitePoint Enthusiast devhen's Avatar
    Join Date
    Jul 2002
    Location
    Salt Lake City, UT
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wow. thanks travis. i wrote this code several months ago and wasn't sure exactly how it all was working. thanks for your input.

  11. #11
    SitePoint Enthusiast
    Join Date
    Nov 2002
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no problem.
    travis


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
  •