Next: The Browser (navigator) Object
Up: Objects in JavaScript
Previous: Some Common window Methods
Two aspects of the form can be manipulated though JavaScript:
- the data that is entered onto your form can be
checked at submission.
- you can actually build forms through JavaScript.
Form Elements:
-
The elements of the form are held in an array
- any of the properties
of those elements now accessed though your
JavaScript.
This example shows a form and a function which reads the properties of
the form elements:
<html>
<head>
<script language="javascript">
function validate() {
var method = document.forms[0].method;
var action = document.forms[0].action;
var value = document.forms[0].elements[0].value;
if(value != "David"){
document.forms[0].reset();
}
else {
alert("Hi David!!");
}
}
</script>
</head>
<body>
<form method="post">
<input type="text" name="user" size=32>
<input type="submit" value="Press Me!"
onClick="validate()">
</form>
</body>
</html>
The main points of this program are:
- We use the onClick event to trigger validation routines.
- This event is set in the form's input tag for the submit type.
- onClick event can be applied to all form elements -- The event is triggered when the user
clicks on that element.
- There other events that can triggered in a form:
- onSubmit
- :
This event can only be triggered by the form itself and occurs when a form is
submitted.
-
onReset
- :
Triggered when
a form is reset by the user.
Further examples on data validation later.
Next: The Browser (navigator) Object
Up: Objects in JavaScript
Previous: Some Common window Methods
Dave Marshall
9/28/2001