Page view counter

Javascript Error: Object Requred - but WHY?

Last post 05-27-2008 7:33 AM by dotdodont. 7 replies.

Sort Posts:

  • Javascript Error: Object Requred - but WHY?

    05-15-2008, 6:00 AM
    • Loading...
    • dotdodont
    • Joined on 03-09-2007, 10:47 AM
    • Posts 97
    • Points 15

    Hi All,

    This is really killing me, one of those, no doubt, simple-to-fix-but-I-just-cannot-see-it problems.

    see code below, on page load the script should change the bg color of the element
    tried it with td and div, but both return error 'Object Required' - but why??

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
        <title>Edit Sales People - OFFICE</title>
        <link rel="stylesheet" type="text/css" href="../include/style.css">
    <script type="text/javascript">

    document.getElementById("test").style.backgroundColor = "#FFFFCC";

    </script>
    </head>
    <body>

    <table>
        <tr>
            <td class="Light">Role</td>
            <td id="x">hello</td>
        </tr>
    </table>

    <div id="test">hellow</div>
    </body>
    </html>  

    Filed under:
  • Re: Javascript Error: Object Requred - but WHY?

    05-15-2008, 6:56 AM
    Answer
    • Loading...
    • ramblor
    • Joined on 03-13-2008, 10:03 AM
    • Posts 1,013
    • Points 6,676

    Use this instead:

    window.onload=function()
    {
        document.getElementById("test").style.backgroundColor = "#FFFFCC";
    }

    At the moment you're trying to get a handle to the div before it's been loaded on the page, so it can't be found.

    "Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us."
  • Re: Javascript Error: Object Requred - but WHY?

    05-15-2008, 9:03 AM
    • Loading...
    • sanjaysantra
    • Joined on 09-28-2006, 2:01 PM
    • Kolkata
    • Posts 55
    • Points 79

    Hi,

    Can you try something like this:

    <div id="test" runat="server">hellow</div>

    Put the following line inside a JavaScript function:

    function SetBGColor()

    {

     document.getElementById("test").style.backgroundColor = "#FFFFCC";

    }

    Just call the function written above as required.

    I hope this may solve your problem.

     

    saanj
    Either you love IT or leave IT.....
  • Re: Javascript Error: Object Requred - but WHY?

    05-15-2008, 9:38 AM
    • Loading...
    • Dollarjunkie
    • Joined on 01-28-2007, 8:18 AM
    • Posts 803
    • Points 1,095

    dotdodont:

    Hi All,

    This is really killing me, one of those, no doubt, simple-to-fix-but-I-just-cannot-see-it problems.

    see code below, on page load the script should change the bg color of the element
    tried it with td and div, but both return error 'Object Required' - but why??

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
        <title>Edit Sales People - OFFICE</title>
        <link rel="stylesheet" type="text/css" href="../include/style.css">
    <script type="text/javascript">

    document.getElementById("test").style.backgroundColor = "#FFFFCC";

    </script>
    </head>
    <body>

    <table>
        <tr>
            <td class="Light">Role</td>
            <td id="x">hello</td>
        </tr>
    </table>

    <div id="test">hellow</div>
    </body>
    </html>  

     

     

    Right off the Bat, I am going to say that the reason why is cause at the point where you have the code, when it executes, the text Div has not been created at all and so it does not recognize it. If you place the code in a function and maybe call it later on, it might be the fix you need in this case.

    .Net Web/Software Engineer
  • Re: Javascript Error: Object Requred - but WHY?

    05-15-2008, 12:16 PM
    • Loading...
    • NC01
    • Joined on 08-26-2005, 3:33 PM
    • Posts 13,275
    • Points 71,391

    Ramblor answered this on the first try and the very best way to solve the problem. I have no idea why others feel the need to add posts with basically the same solutions.

    NC...

  • Re: Javascript Error: Object Requred - but WHY?

    05-15-2008, 1:41 PM
    • Loading...
    • dotdodont
    • Joined on 03-09-2007, 10:47 AM
    • Posts 97
    • Points 15

    thanks for ALL the responses, I just thought the script would automatically execute anyway. but window.onload does the trick.

     

    thanks again 

  • Re: Javascript Error: Object Requred - but WHY?

    05-18-2008, 10:28 AM
    • Loading...
    • NC01
    • Joined on 08-26-2005, 3:33 PM
    • Posts 13,275
    • Points 71,391

    dotdodont:

    thanks for ALL the responses, I just thought the script would automatically execute anyway. but window.onload does the trick.

     

    thanks again 

    The script is execution "automatically". The problem is that at the point that the script is executing, there is no div element named "test" because the test div is declared BELOW where the script executes. Putting the script in the window.onload event makes it not execute until all elements are declared.

    NC...

  • Re: Javascript Error: Object Requred - but WHY?

    05-27-2008, 7:33 AM
    • Loading...
    • dotdodont
    • Joined on 03-09-2007, 10:47 AM
    • Posts 97
    • Points 15

     of course, thanks I should've known.

Page 1 of 1 (8 items)