< Back

Document Subject: Test if field exists in javascript
Hint Short Cut: Add this to your code & documentation to help you find this page.
http://#JSFieldExists or http://A555F9/nn.nsf/ByAlias/JSFieldExists

In HTML form validation, it is good to test if a field exists before trying  to check the value for submitting etc.




 

Grab the elementbyid, if it returns false then the field does not exist.

 

var f = document.forms[0];

var LinkaExists = document.getElementById('Linka');
var LinkbExists = document.getElementById('Linkb');

if ( LinkbExists )
{
   f.Linkb.focus();
}
else if ( LinkaExists )
{
   f.Linka.focus();
}

 

You can then also check the value depending on the type of the field, using f.type

eg: LinkaExists.type.lowercase=='text' or 'textarea' or 'checkbox' or 'select-one' etc.