How to check whether an HTML form variable exists

Christopher Koenigsberg

I also use this code now, which I found somewhere through another jguru JavaScript forum message recently, to test if something is a valid Javascript object (e.g. if typeof is not "undefined"), rather than just test if it is null. I was getting runtime javascript errors, in the browser, before adding this additional test; this is used in code where I'm trying to do stuff like set and clear checkboxes or radio buttons or text field values, in form fields, from event handlers on each other; the errors would occur where I tried to use the value of something that was not null, but it did indeed have "undefined" for typeof:


	function testIsValidObject(objToTest) {
		if (null == objToTest) {
			return false;
		}
		if ("undefined" == typeof(objToTest) ) {
			return false;
		}
		return true;

	}

0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact