Java Reference
In-Depth Information
var elementCount = 0;
var theForm = document.form1;
while(elementCount =<= theForm.length) {
if (theForm.elements[elementcount].type == "text") {
if (theForm.elements[elementCount].value() = "")
alert("Please complete all form elements");
theForm.elements[elementCount].focus;
e.preventDefault();
break;
}
}
}
document.form1.addEventListener("submit", checkForm);
</script>
</body>
</html>
exercise 2 Solution
The bugā€free version looks like this:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Chapter 18: Question 2</title>
</head>
<body>
<form name="form1" action="">
<input type="text" id="text1" name="text1" />
<br />
CheckBox 1<input type="checkbox" id="checkbox2" name="checkbox2" />
<br />
CheckBox 1<input type="checkbox" id="checkbox1" name="checkbox1" />
<br />
<input type="text" id="text2" name="text2" />
<p>
<input type="submit" value="Submit" id="submit1"
name="submit1" />
</p>
</form>
<script>
function checkForm(e) {
var elementCount = 0;
var theForm = document.form1;
while(elementCount < theForm.length) {
if (theForm.elements[elementCount].type == "text") {
if (theForm.elements[elementCount].value == "") {
alert("Please complete all form elements");
theForm.elements[elementCount].focus();
Search WWH ::




Custom Search