Displaying remainder of two values on the page
Posted: Tue Feb 08, 2011 3:29 am
My code below determines the remainder of field1 - field2. The remainder is then written to another input field. From the example you will see i use onFocus() to display the remainder in the input field. My question : Is it possible to have the value written to the 'remainder' input field when a value has been entered into the 'field2' field (without clicking on the remainder field), and how? I've tried using onFocus and onBlur with the second field but it doesn't yield the results I am after.
An additional question, if i want to write the remaining value to the page, not into a textbox, how do i go about it?
Code: Select all
<script type="text/javascript">
function calculate_remainder()
{
var first_number = window.document.form1.fNumber.value;
var second_number = window.document.form1.sNumber.value;
var remainder_number;
remainder_number = first_number - second_number;
window.document.form1.remainder.value = remainder_number;
}
/* */
</script>
<form action="" method="post" name="form1" >
First value : <input type="text" name="first_number" id="fNumber" />
<br />
Second value : <input type="text" name="second_number" id="sNumber" />
<br />
Remainder of value 1 - value 2<input type="text" name="remainder" id="remainder" value=""
onFocus="calculate_remainder();" />
<br />
<input type="submit" name="btn" value="Go" />
</form>