Java Reference
In-Depth Information
The next error is the failure to put your block of
if
code in curly braces. Even though JavaScript
won't throw an error because the syntax is fine, the logic is not so fine, and you won't get the results
you expect. With the braces, the
if
statement should be as follows:
if (theForm.elements[elementCount].value == "") {
alert("Please complete all form elements")
theForm.elements[elementCount].focus;
formValid = false;
break;
}
The penultimate error is in this line:
theForm.elements[elementCount].focus;
This time you have a method but with no parentheses after it. Even methods that have no
parameters must have the empty parentheses after them if you intend to execute that method. So,
corrected, the line is as follows:
theForm.elements[elementCount].focus();
Now you're almost done; there is just one more error. This time it's not something wrong with
what's there, but rather something very important that should be there but is missing. What is it? It's
this:
elementCount++;
This line should be in your
while
loop, otherwise
elementCount
will never go above
0
and the
while
loop's condition will always be
true
, resulting in the loop continuing forever: a classic infinite
loop.