Uses the "onClick" event handler; parses two number provided in the input fields and adds or subtracts them, depending on which button is clicked.
Here's the code:
<form action="javascript:;" name="form3" method="post">
<p>Enter a number here:<br />
<input type="text" length="30" name="textInput3" maxlength="25" /><br />
<p>Enter another number here:<br />
<input type="text" length="30" name="textInput4" maxlength="25" /><br />
<input type="button" name="AddButton" value="Add Numbers"
onclick="var sum = parseFloat(textInput3.value) + parseFloat(textInput4.value);
alert('The sum is ' + sum + '.')" >
<input type="button" name="SubtractButton" value="Subtract Numbers"
onclick="var result = parseFloat(textInput3.value) - parseFloat(textInput4.value);
alert('The difference is ' + result + '.')" >
<input type="reset" name="reset" value="Clear Form">
</form>
In principle, this little script contains almost everything we have to understand in order to do our projects:
parseFloat( string ) is a JavaScript built-in function; it accepts a string and returns a floating point number. If the input is not a number, it returns NaN, which stands for "not a number".var that precedes the variable name the first time it occurs.