364,083 Members | 5675 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

Check if object exists?

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

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


[on]
P: n/a
[on]

Chris Ashley wrote:[color=blue]
> 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...[/color]

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

Randy Webb
P: n/a
Randy Webb
Chris Ashley said the following on 4/13/2006 5:26 AM:[color=blue]
> How do I check if an object exists in Javascript?[/color]

With an if test.
[color=blue]
> EG:
>
> if (document.getElementById("product").value == null)[/color]

What element has an id of product and does that element have a .value
property?
[color=blue]
> {
> prodValue = "";
> }
> else
> {
> prodValue = document.getElementById("product").value;
> }
>
> This gives me an 'object required' error...
>[/color]

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

MB
P: n/a
MB
"Chris Ashley" <chris.ashley2@gmail.com> skrev i meddelandet
news:1144920410.073426.93870@u72g2000cwu.googlegro ups.com...[color=blue]
> 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...
>[/color]

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


Apr 13 '06 #4

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

why not

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

Apr 13 '06 #5

Dr John Stockton
P: n/a
Dr John Stockton
JRS: In article <123t0hatj3vcj84@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 :[color=blue]
>
>why not
>
>if (document.getElementById('product')) {
> // code here when the element exists
>}
>[/color]


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

Randy Webb
P: n/a
Randy Webb
Dr John Stockton said the following on 4/14/2006 1:30 PM:[color=blue]
> JRS: In article <123t0hatj3vcj84@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 :[color=green]
>> why not
>>
>> if (document.getElementById('product')) {
>> // code here when the element exists
>> }
>>[/color]
>
>
> 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
>[/color]

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

[on]
P: n/a
[on]

Tony wrote:[color=blue]
> [on] wrote:[color=green]
> >
> > <code>
> > var productElement = document.getElementById("product");
> > if (productElement != null)
> > {
> > // Code here when the Element Exists.
> > }
> > </code>[/color]
>
> why not
>
> if (document.getElementById('product')) {
> // code here when the element exists
> }[/color]

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

Thomas 'PointedEars' Lahn
P: n/a
Thomas 'PointedEars' Lahn
[on] wrote:
[color=blue]
> Tony wrote:[color=green]
>> [on] wrote:[color=darkred]
>> > <code>
>> > var productElement = document.getElementById("product");
>> > if (productElement != null)
>> > {
>> > // Code here when the Element Exists.
>> > }
>> > </code>[/color]
>>
>> why not
>>
>> if (document.getElementById('product')) {
>> // code here when the element exists
>> }[/color]
>
> 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.[/color]

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

Tony
P: n/a
Tony
Thomas 'PointedEars' Lahn wrote:[color=blue]
> [on] wrote:
>[color=green]
>>Tony wrote:
>>[color=darkred]
>>>[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
>>>}[/color]
>>
>>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.[/color]
>
> 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.[/color]

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.
[color=blue]
> 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 ...
> }[/color]

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

(just for clarity)
Apr 19 '06 #10

Post your reply

Help answer this question



Didn't find the answer to your JavaScript / Ajax / DHTML question?

You can also browse similar questions: JavaScript / Ajax / DHTML