Handling undefined elements in Javascript

Some times you may verify the element for null but if the element is undefined then null condition fails so best practice is to check for null and undefined.

checking for null

if(document.getElementsByName("TxtElement1")  == null)
alert("the element is null");

checking for undefined

if(document.getElementsByName("TxtElement1").value == 'undefined')
alert("the element is undefined");


About kakani santosh kumar

Working as Software Engineer in Prithvi Information Solutions having 3 years of Experience in Dotnet Framework ranging from 1.1 to 3.5
This entry was posted in Uncategorized and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s