Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 181
  • Last Modified:

Javascript Error when field is not on the page.

I have a piece of code (see below) that is supposed to hide a field on our website.  See this thread here:

http://www.experts-exchange.com/Programming/Languages/Scripting/Q_22993842.html

It works fine for the particular page that has the field on it, but all the other pages get an "Object Required" error because the script can't find it.  Is there a way to prevent this? IE: have the script check to see if the field exists or not.  I'm forced to put the script in the footer because this is a hosted page that I have limited access to.

Thanks!
<script type="text/javascript">
    document.getElementById('partner').style.display = 'none';
</script>

Open in new window

0
BasilFawlty001
Asked:
BasilFawlty001
  • 3
1 Solution
 
william007Commented:


try this: 
 
   <script type="text/javascript">
    if(document.getElementById('partner')){
      document.getElementById('partner').style.display = 'none';
    }
   </script>
 
this is to check whether the element exists before calling the setting the display property

Open in new window

0
 
william007Commented:
Sorry, a bit mess up, just reorganized it :)
Try the following code:
This is to check whether the element exists before calling the setting the display property
   <script type="text/javascript">
    if(document.getElementById('partner')){
      document.getElementById('partner').style.display = 'none';
    }
   </script>

Open in new window

0
 
BasilFawlty001Author Commented:
Thanks!
0
 
william007Commented:
No problem, glad it helps :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now