Check if a form input exists
You are missing a bracket: if(!document.getElementsByName("field2"))
Actually, the problem was that the page had various forms and therefore forms[0]
was not referring to the form I wanted. So I think the best way is to use this
and refer to the input
field directly. Also, it is clearer to compare to undefined
rather than !
.
This works:
function foobar(fooform){
if (fooform.field2 === undefined) {
alert("foobar");
}
}
Called like this:
foobar(this.form);