"Object not found" error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tarantulus
    New Member
    • May 2007
    • 114

    "Object not found" error

    Code:
    if("dept"=='dept'){
    	document.getElementById('add').style.display='none';
    };
    
    <DIV id="add">
    
    <some html here..>
    
    </DIV>
    this gives me an "object not found" error, any ideas what could be causing it?

    (PS it might seem odd check if dept is the same as dept, but the first "dept" is actually a PHP variable)
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    I've changed the thread title for you. Please use a good thread title. Thanks!

    Is this how your code appears? The Javascript should be separate from the HTML within script tags.

    Comment

    • Tarantulus
      New Member
      • May 2007
      • 114

      #3
      Originally posted by acoder
      I've changed the thread title for you. Please use a good thread title. Thanks!

      Is this how your code appears? The Javascript should be separate from the HTML within script tags.
      My apologies.. thanks for changing the title.

      I left out the script tags they encapsualte the JS and the <DIV> comes after as it well should

      Code:
      <script>
      
      Javascript..
      
      </script>
      <div>

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Is this Javascript within a function? If not, that may (rather, will) be your problem. It comes across the Javascript and tries to set the display property to 'none', but it hasn't come across an object with an id of "add" yet, hence the Object not found error.

        To solve this, run this code on onload or after the div. Alternatively, use your PHP code to check that the PHP variable is equal to 'dept'. If it is, set the display property to none inline:
        [PHP]<?php if ($phpvar == 'dept') echo "style='display :none';"; ?>[/PHP]

        Comment

        • ronnil
          Recognized Expert New Member
          • Jun 2007
          • 134

          #5
          you problem is probably that you try to access your div tag before it is actually instantiated.

          there are two immediate solutions to this problem

          1. Set your script below the div tag. Then it should load like it's supposed to

          2. Put you execution of the script inside a window load event (this is the best way)

          e.g.
          Code:
          <html>
          <head>
          <!-- header stuff here -->
          <script language="javascript">
          
          
          function windowOnLoad()
          {
              var divElement = document.getElementById('myDiv');
              //do stuff with your div here
          }
          
          try{
          	window.attachEvent('onload',windowOnLoad); //MSIE
          } catch(e) { 
              try{
          	window.addEventListener('load,windowOnLoad,false); //Proper browser
              } catch(e) {
                  alert('Your browser is really old');
              }
          } 
          
          </script>
          </head>
          <body>
          <div id="myDiv">
          </div>
          </body>
          </html>

          and a little advice: try to always acces your element with document.getEle mentById, this ensures better browsercompabil ity :)

          Comment

          • Tarantulus
            New Member
            • May 2007
            • 114

            #6
            DOH! of course, you're right... I'm such an idiot sometimes.

            thanks peeps

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              Originally posted by ronnil
              [CODE=html]
              ...<script language="javas cript">
              function windowOnLoad()
              {
              var divElement = document.getEle mentById('myDiv ');
              //do stuff with your div here
              }

              try{
              window.attachEv ent('onload',wi ndowOnLoad); //MSIE
              } catch(e) {
              try{
              window.addEvent Listener('load, windowOnLoad,fa lse); //Proper browser
              } catch(e) {
              alert('Your browser is really old');
              }
              }

              </script>
              </head>
              [/CODE]
              Similar replies and a good example. Just one or two points though:
              • The "language" attribute of the script tag is deprecated. You should use [code=html]type="text/javascript[/code] instead.
              • This is a personal preference and it won't really make much of a difference, but I would put the standard method first before the IE-only method.

              Comment

              Working...