[Javascript] Check if ID exist

João Cândido de Souza Neto joao at curitibaonline.com.br
Fri Aug 18 13:43:00 CDT 2006


elements = document.getElementsById("msgsubject");

if (elements.length>0)
    ...
}

I think it´ll work fine.

João Cândido de Souza Neto
Curitiba Online
joao at curitibaonline.com.br
(41) 3324-2294 (41) 9985-6894
http://www.curitibaonline.com.br
----- Original Message ----- 
From: "Peter Lauri" <lists at dwsasia.com>
To: "'[JavaScript List]'" <javascript at LaTech.edu>
Sent: Friday, August 18, 2006 3:37 PM
Subject: [Javascript] Check if ID exist


> How do I check if an element with a specific ID exists?
>
> Before I call a function I need to check if some elements are in the
> document already.
>
> Right now I do:
>
> isformstart = false;
>
> try {
>   if(document.getElementById('msgsubject').value!="") isformstart = true;
>   if(document.getElementById('msgtext').value!="") isformstart = true;
>   if(isformstart) return confirm("You have started with a new message,
> are you sure you want to cancel this message. All text will be lost.");
>   else return true;
> } catch (e) {
>   return true;
> }
>
> Is this maybe the way to go? Try to do, and then just handle exceptions?
>
>
>
>
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript
> 




More information about the Javascript mailing list