[Javascript] Check if ID exist

Peter Lauri lists at dwsasia.com
Fri Aug 18 13:37:39 CDT 2006


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?







More information about the Javascript mailing list