473,657 Members | 2,523 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

getElementById "Object required" error

I've got what should be a simple assignment of either an element value
or a default string to a variable, but when the element doesn't exist I
get an "Object required" error rather than an assignment of the default
value.

I originally used the simple statement:

var v = document.getEle mentById('foo') .value ||
parent.document .getElementById ('foo').value ||
'unknown';
document.writel n(v);

This caused the "Object required" error if there was no element, so I
added some error checking:

var v = (document.getEl ementById)
? document.getEle mentById('foo') .value
: (parent.documen t.getElementByI d)
? parent.document .getElementById ('foo').value
: 'unknown';
document.writel n(v);

Unfortunately, this still results in an "Object required" error when
the element is not present in the document or its parent.

Any pointers to where I am going wrong would be appreciated.
Thanks,
Dave H.

Jul 23 '05 #1
2 24657


Dave Hammond wrote:

var v = document.getEle mentById('foo') .value ||
parent.document .getElementById ('foo').value ||
'unknown';
document.writel n(v);

This caused the "Object required" error if there was no element


If there is no element with id 'foo' then getElementById returns null
and you can't access null.value so you need to make sure you check the
return value of getElementById e.g.
var element;
if (document.getEl ementById && (element =
document.getEle mentById('foo') )) {
// now access element.value here
}

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2
Thanks, Martin, that did the trick!

-Dave H.

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
36694
by: deko | last post by:
I'm getting intermittent "Object Invalid or No Longer Set" errors in my Access 2002 mdb. What causes these errors? Has anyone dealt with this before? I can't trace it because it's not easy to duplicate -- it just show up... I've heard that spurious queries that use Now() or Date() or some other date
6
19936
by: Lauchlan M | last post by:
Hi. Usin ASP.NET, getting an "Object reference not set to an instance of an object" error. In my login.aspx page I have: string arrUserRoles = new string {"UserRole"}; Context.Items.Add("UserRoles", arrUserRoles); Context.User = new
1
2351
by: Lauchlan M | last post by:
Hi. I'm using ASP.NET, getting an "Object reference not set to an instance of an object" error. In my login.aspx page I have: string arrUserRoles = new string {"UserRole"}; Context.Items.Add("UserRoles", arrUserRoles); Context.User = new
2
2973
by: louie.hutzel | last post by:
This JUST started happening, I don't remember changing any code: When I click the submit button on my form, stuff is supposed to happen (which it does correctly) and a result message is posted back to the same page at the top of my form alerting the user that their request has been approved or denied. However, the end result is an error message: Error Message: Object reference not set to an instance of an object.
4
2365
by: Hansen | last post by:
Hi! I have a try/catch statement on my webpage, in which a create som html. Sometimes this routine fails and the catch writes the error in an alert box. My problem is that the only thing the error says is even though I write all the attributes in the Error object (name, number, description, message). How can I inspect further what has gone wrong? /Hansen
7
443
by: imatts | last post by:
Hi can anyone help with this little problem. I have a simple script to swap between two divs on a page. It works perfectly in Firefox & Safari & Opera. It fails in IE 6 giving Object Required error on initializing second variable var layer2. I can't get to the bottom of this. Can anyone help? <script type="text/javascript"> function showhide(element) { var layer1 = document.getElementById('layer1').style; var layer2 =...
3
3979
by: krishnagovindaraj | last post by:
Hi All, I have to display the value in textbox that is selected from dropdownlistbox using ajax.i got the readyState is null or not an object error. coding: <script language="javascript" type="text/javascript"> function GetDataViaAJAX() {
6
4952
by: cleary1981 | last post by:
I have adapted code from http://dunnbypaul.net/js_mouse/ I want to use a button to create new draggable divs but i keep getting error "is null or not an object" heres the code <html> <head> <title>Drag and drop module</title> <style type="text/css"> #canvas {
10
7607
by: andersond | last post by:
On a webpage that has a variety of questions I get an "object required" error on lines like this... document.getElementById('tableQuestion17').style.visibility="visible"; This is the code for the table "tableQuestion17" <table border="0" width="100%" id="tableQuestion17" cellpadding="0" style="visibility:hidden">
0
8316
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8737
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8509
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6174
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5636
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4327
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1967
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1730
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.