Solved

Javascript Error when field is not on the page.

Posted on 2007-12-05
4
172 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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…

710 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