471,336 Members | 1,231 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,336 software developers and data experts.

"Object not found" error

Tarantulus
114 100+
Expand|Select|Wrap|Line Numbers
  1. if("dept"=='dept'){
  2.     document.getElementById('add').style.display='none';
  3. };
  4.  
  5. <DIV id="add">
  6.  
  7. <some html here..>
  8.  
  9. </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)
Aug 20 '07 #1
6 3472
acoder
16,027 Expert Mod 8TB
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.
Aug 20 '07 #2
Tarantulus
114 100+
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

Expand|Select|Wrap|Line Numbers
  1. <script>
  2.  
  3. Javascript..
  4.  
  5. </script>
  6. <div>
Aug 20 '07 #3
acoder
16,027 Expert Mod 8TB
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]
Aug 20 '07 #4
ronnil
134 Expert 100+
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.
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <!-- header stuff here -->
  4. <script language="javascript">
  5.  
  6.  
  7. function windowOnLoad()
  8. {
  9.     var divElement = document.getElementById('myDiv');
  10.     //do stuff with your div here
  11. }
  12.  
  13. try{
  14.     window.attachEvent('onload',windowOnLoad); //MSIE
  15. } catch(e) { 
  16.     try{
  17.     window.addEventListener('load,windowOnLoad,false); //Proper browser
  18.     } catch(e) {
  19.         alert('Your browser is really old');
  20.     }
  21.  
  22. </script>
  23. </head>
  24. <body>
  25. <div id="myDiv">
  26. </div>
  27. </body>
  28. </html>
  29.  

and a little advice: try to always acces your element with document.getElementById, this ensures better browsercompability :)
Aug 20 '07 #5
Tarantulus
114 100+
DOH! of course, you're right... I'm such an idiot sometimes.

thanks peeps
Aug 20 '07 #6
acoder
16,027 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. ...<script language="javascript">
  2. function windowOnLoad()
  3. {
  4.     var divElement = document.getElementById('myDiv');
  5.     //do stuff with your div here
  6. }
  7.  
  8. try{
  9.     window.attachEvent('onload',windowOnLoad); //MSIE
  10. } catch(e) { 
  11.     try{
  12.     window.addEventListener('load,windowOnLoad,false); //Proper browser
  13.     } catch(e) {
  14.         alert('Your browser is really old');
  15.     }
  16.  
  17. </script>
  18. </head>
  19.  
Similar replies and a good example. Just one or two points though:
  • The "language" attribute of the script tag is deprecated. You should use
    Expand|Select|Wrap|Line Numbers
    1. type="text/javascript
    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.
Aug 20 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

8 posts views Thread by webgenius | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.