Important thing we have to know before we write a javascript form is "The values are are reading must be with the tag". The values which are in between them can only be read. IF the elements are outer than this form then that variables are not accessible.
Simple example:
<html>
<head>
<title>A Very Simple Calculator</title>
<script type = "text/javascript">
<!-- hide me from older browsers
function multiplyTheFields()
{
var number_one = window.document.the_form.field_one.value;
var number_two = window.document.the_form.field_two.value;
var product = number_one * number_two;
window.document.the_form.the_answer.value = product;
}
// show me -->
</script>
</head>
<body>
<form name = "the_form">
Number 1: <input type = "text" name = "field_one"> <br>
Number 2: <input type = "text" name = "field_two"> <br>
The Product: <input type = "text" name = "the_answer"> <br>
<a href = "#" onClick = "multiplyTheFields(); return false;">Multiply them!</a>
</form>
</body>
</html>
Here is a simple example which takes the values from the two text boxes .. mutiply them and display the resultant value to the third text box.
Note: In the last step of the JavaScript function we see return false. Here return false is mentioned because the form should not be posted and page should not be refreshed.
No comments:
Post a Comment