By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,784 Members | 3,274 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,784 IT Pros & Developers. It's quick & easy.

Check if object exists?

P: n/a
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...

Apr 13 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a

Chris Ashley wrote:
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>

Apr 13 '06 #2

P: n/a
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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Apr 13 '06 #3

P: n/a
MB
"Chris Ashley" <ch***********@gmail.com> skrev i meddelandet
news:11*********************@u72g2000cwu.googlegro ups.com...
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;
}
Apr 13 '06 #4

P: n/a
[on] wrote:

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


why not

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

Apr 13 '06 #5

P: n/a
JRS: In article <12*************@corp.supernews.com>, dated Thu, 13 Apr
2006 09:56:00 remote, seen in news:comp.lang.javascript, 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

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Apr 14 '06 #6

P: n/a
Dr John Stockton said the following on 4/14/2006 1:30 PM:
JRS: In article <12*************@corp.supernews.com>, dated Thu, 13 Apr
2006 09:56:00 remote, seen in news:comp.lang.javascript, 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


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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Apr 14 '06 #7

P: n/a

Tony wrote:
[on] wrote:

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


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.

Apr 18 '06 #8

P: n/a
[on] wrote:
Tony wrote:
[on] wrote:
> <code>
> var productElement = document.getElementById("product");
> if (productElement != null)
> {
> // Code here when the Element Exists.
> }
> </code>


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.


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
Apr 18 '06 #9

P: n/a
Thomas 'PointedEars' Lahn wrote:
[on] wrote:
Tony wrote:
[on] wrote:

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

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.


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.


I think it ultimately depends on what you're doing in the // code here
when the element exists - after all, it IS possible that you're not
doing something to that element :)

But - point taken. And that's something I may want to take a look at in
my own code.
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 ...
}


Would that be:
var productElement = document.getElementById('product');
if (productElement) ...

(just for clarity)
Apr 19 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.