Solved

Javascript Error when field is not on the page.

Posted on 2007-12-05
4
170 Views
Last Modified: 2010-04-16
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
Comment
Question by:BasilFawlty001
  • 3
4 Comments
 
LVL 9

Expert Comment

by:william007
ID: 20412346


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
 
LVL 9

Accepted Solution

by:
william007 earned 500 total points
ID: 20412363
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
 

Author Comment

by:BasilFawlty001
ID: 20412417
Thanks!
0
 
LVL 9

Expert Comment

by:william007
ID: 20412450
No problem, glad it helps :)
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question