I have a Javascript file that I use to check the input of a form. However, when I uncomment the commented line, the javascript file does not function properly, and the form is submitted even if some of the fields are blank. In fact, the Javascript file doesn’t even make it to the second alert function. What is going on?
function checkLogin()
{
alert("HERE");
//document.getElementById("errors").style.display = "none";
var form = document.getElementById("form1");
var problems = false;
document.getElementById("errorJS").style.display = "none";
document.getElementById("errorJS").innerHTML = "";
alert("test");
if(form.email.value.length < 1)
{
document.getElementById("errorJS").innerHTML += "Please enter your email address. <br />";
problems = true;
document.getElementById("errorJS").style.display = "block";
}
if(form.pword.value.length < 1)
{
document.getElementById("errorJS").innerHTML += "Please enter your password.";
problems = true;
document.getElementById("errorJS").style.display = "block";
}
alert(!(problems));
return !(problems);
}
That was it, the line was returning null. Thank you for the answers.
Though you don’t exactly define what “does not function properly” means, and you have provided no error info (which the browser surely supplied to you), I would say that
document.getElementById("errors")is returning null. This would mean that no element exists in your document with the ID oferrors.A check of whether
document.getElementById("errors")returned something usable before attempting to access properties thereon would be a good step forward. Ensuring your document is as you assume it to be would be another.