how to check if an object has already been created

  • Follow


How can I tell if a div has already been created?  I'd like to update it if  
it's already existing, create it otherwise.  I'm getting errors that the 
object has no property.

var newdiv = document.getElementById('newDiv_');

if (typeof newdiv == "undefined"){
   /* create it */
   /* set to defaults */
}else{
   /* update existing one */
}


0
Reply Tony 6/14/2007 9:45:24 PM

On Jun 14, 5:45 pm, Tony Rice <e...@email.com> wrote:
> How can I tell if a div has already been created?  I'd like to update it if
> it's already existing, create it otherwise.  I'm getting errors that the
> object has no property.
>
> var newdiv = document.getElementById('newDiv_');
>
> if (typeof newdiv == "undefined"){
>    /* create it */
>    /* set to defaults */}else{
>
>    /* update existing one */
>
> }

It looks like what you are doing is correct, but instead of first
setting the object to a variable you should test it first:

if (typeof document.getElementById("newDiv") == "undefined") {
    /* ... create it ... */
} else {
    /* ... do other stuff ... */
}

Unless someone else has a better way, that's how I'd do it. Hope that
helps.

-Charles

0
Reply Charles 6/14/2007 9:56:32 PM


"Charles G." <cgnost@gmail.com> wrote in
news:1181858192.421170.182330@j4g2000prf.googlegroups.com: 

> On Jun 14, 5:45 pm, Tony Rice <e...@email.com> wrote:
>> How can I tell if a div has already been created?  I'd like to update
>> it if it's already existing, create it otherwise.  I'm getting errors
>> that the object has no property.
>>
>> var newdiv = document.getElementById('newDiv_');
>>
>> if (typeof newdiv == "undefined"){
>>    /* create it */
>>    /* set to defaults */}else{
>>
>>    /* update existing one */
>>
>> }
> 
> It looks like what you are doing is correct, but instead of first
> setting the object to a variable you should test it first:
> 
> if (typeof document.getElementById("newDiv") == "undefined") {
>     /* ... create it ... */
> } else {
>     /* ... do other stuff ... */
> }
> 
> Unless someone else has a better way, that's how I'd do it. Hope that
> helps.


That worked, thanks.

0
Reply Tony 6/15/2007 12:34:35 AM

On Jun 14, 5:34 pm, Tony Rice <e...@email.com> wrote:
> "Charles G." <cgn...@gmail.com> wrote innews:1181858192.421170.182330@j4g2000prf.googlegroups.com:
>
>
>
> > On Jun 14, 5:45 pm, Tony Rice <e...@email.com> wrote:
> >> How can I tell if a div has already been created?  I'd like to update
> >> it if it's already existing, create it otherwise.  I'm getting errors
> >> that the object has no property.
>
> >> var newdiv = document.getElementById('newDiv_');
>
> >> if (typeof newdiv == "undefined"){
> >>    /* create it */
> >>    /* set to defaults */}else{
>
> >>    /* update existing one */
>
> >> }
>
> > It looks like what you are doing is correct, but instead of first
> > setting the object to a variable you should test it first:
>
> > if (typeof document.getElementById("newDiv") == "undefined") {
> >     /* ... create it ... */
> > } else {
> >     /* ... do other stuff ... */
> > }
>
> > Unless someone else has a better way, that's how I'd do it. Hope that
> > helps.
>
> That worked, thanks.

It did!!??

If the document does not contain an element with id ""newDiv",
document.getElementById("newDiv") will return a null object.

The typeof a null object is "object", which clearly is not === (or
even ==) to "undefined".

You may fare better with:

  var divEl = document.getElementById("newDiv");
  if (! divEl) {
     /* create it */
     /* set to defaults */}
  }
  else {
     /* update existing one */
  }

--
  ../rh

0
Reply ron 6/15/2007 1:23:31 AM

3 Replies
430 Views

(page loaded in 0.043 seconds)

Similiar Articles:













7/25/2012 4:46:32 PM


Reply: