[Javascript] Check if ID exist

Paul Novitski paul at juniperwebcraft.com
Fri Aug 18 13:49:45 CDT 2006


At 11:37 AM 8/18/2006, Peter Lauri wrote:
>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.


The DOM method getElementById(id) returns null if the identified 
element doesn't exist.  Therefore you can code:

         if (!document.getElementById('msgsubject')) return;
or:
         if (!document.getElementById('msgsubject')) return 
alert("Error: 'msgsubject' not found");
or:
         if (!document.getElementById('msgsubject')) return 
registerError("msgsubject", "not found");
etc.

Regards,
Paul 




More information about the Javascript mailing list