DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Stephen has posted 20 posts at DZone. View Full User Profile

Calculating Sum Of 2 Form Fields Using Javascript

10.31.2008
| 30202 views |
  • submit to reddit
        Automatically adds two fields together (text fields), without the need for a submit or input button.

<script type="text/javascript"><!--
function updatesum() {
document.form.sum.value = (document.form.sum1.value -0) + (document.form.sum2.value -0);
}
//--></script>

<body>

<form name="form" >
Enter a number:
<input name="sum1" onChange="updatesum()" />
and another number:
<input name="sum2" onChange="updatesum()" />
Their sum is:
<input name="sum" readonly style="border:0px;">
</form>

</body>