Check if object exists?

C

Chris Ashley

How do I check if an object exists in Javascript?

EG:

if (document.getElementById("product").value == null)
{
prodValue = "";
}
else
{
prodValue = document.getElementById("product").value;
}

This gives me an 'object required' error...
 
O

[on]

Chris said:
How do I check if an object exists in Javascript?

EG:

if (document.getElementById("product").value == null)
{
prodValue = "";
}
else
{
prodValue = document.getElementById("product").value;
}

This gives me an 'object required' error...

So the element with the id "product" might not exist ?

var productElement = document.getElementById("product");

productElement should now either be the element or null, if it's null
you can't check "value" for it.

so:

<code>
var productElement = document.getElementById("product");
if (productElement != null)
{
// Code here when the Element Exists.
}
</code>
 
M

MB

Chris Ashley said:
How do I check if an object exists in Javascript?

EG:

if (document.getElementById("product").value == null)
{
prodValue = "";
}
else
{
prodValue = document.getElementById("product").value;
}

This gives me an 'object required' error...

if (document.getElementById("product") == undefined)
{
prodValue = "";
}
else
{
prodValue = document.getElementById("product").value;
}
 
R

Randy Webb

Chris Ashley said the following on 4/13/2006 5:26 AM:
How do I check if an object exists in Javascript?

With an if test.
EG:

if (document.getElementById("product").value == null)

What element has an id of product and does that element have a .value
property?
{
prodValue = "";
}
else
{
prodValue = document.getElementById("product").value;
}

This gives me an 'object required' error...

Then you probably do not have an ID of "product", or, that element does
not have a .value property.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Thu, 13 Apr
2006 09:56:00 remote, seen in Tony <tony23@ds
lextreme.WHATISTHIS.com> posted :
why not

if (document.getElementById('product')) {
// code here when the element exists
}


or, untested but for efficiency,

if ( T = document.getElementById('product') ) {
// code herein when the element exists, using T
}

or

if ( !(T = document.getElementById('product') ) ) { AbandonShip() }
// code hereon when the element exists, using T
 
R

Randy Webb

Dr John Stockton said the following on 4/14/2006 1:30 PM:
JRS: In article <[email protected]>, dated Thu, 13 Apr
2006 09:56:00 remote, seen in Tony <tony23@ds
lextreme.WHATISTHIS.com> posted :


or, untested but for efficiency,

if ( T = document.getElementById('product') ) {
// code herein when the element exists, using T
}

or

if ( !(T = document.getElementById('product') ) ) { AbandonShip() }
// code hereon when the element exists, using T

The one drawback/possible failure would be in IE where you have an
element with a name or ID of T, then it would clash and crash. Other
than that, its ok code.
 
O

[on]

Tony said:
why not

if (document.getElementById('product')) {
// code here when the element exists
}

Mostly to show the user what's returning "null", and he wanted to grab
a value of the element later on. Though I could do what Dr Sockton
said, but I just don't "script/program" like that.
 
T

Thomas 'PointedEars' Lahn

Mostly to show the user what's returning "null", and he wanted to grab
a value of the element later on. Though I could do what Dr Sockton
said, but I just don't "script/program" like that.

The reason is a different one. If you do the latter test, you will
have to retrieve the reference for the element object, i.e. call
document.getElementById() twice for the same argument. That is
unnecessarily inefficient.

However, comparing against `null' is not necessary, and, more important, not
complete. The method called is defined in W3C DOM Level 2+ to return
`null' if there is no such element. However, it is not defined what to
return if there is more than one element that applies (the return value is
defined to be undefined; this must be strictly distinguished from the
`undefined' value of ECMAScript implementations). Still it is reasonable
to assume that it will not be a true-value then. So the test should be a
type-converting one instead:

if (productElement)
{
... productElement ...
}


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top