Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding Form Field Values Together

Status
Not open for further replies.

sjf

Technical User
Jan 1, 2001
56
AU
Hi, I am a complete newbie to Javascript so this may be a very bad question .....

I want to add / minus / multiply / divide two values entered into a form and display an answer to the screen once a "Calculate" button has been pressed without submitting the form the values are being entered in .... any suggestions?
sjf
 
The values are entered as Strings, so first you'll need to convert them to numbers. like:

var number = parseFloat("1234.54");

or you can parse to Integers using parseInt(). These will be converted back to strings for you when you put them back in the form.

<script>
function upDate(form){
var result = form.result;
var item_1 = parseFloat(form.txt_1.value);
var item_2 = parseFloat(form.txt_2.value);
// Do your thing with the numbers
result.value = item_1 + item_2;
}
</script>


<FORM>
<input type=&quot;text&quot; name=&quot;txt_1&quot;>
<input type=&quot;text&quot; name=&quot;txt_2&quot;>
<input type=&quot;text&quot; name=&quot;result&quot;>
<input type=&quot;button&quot; name=&quot;go&quot; value=&quot;Click&quot; onClick=&quot;upDate(this.form)&quot;>
</FORM>

b[sup]2[/sup] - benbiddington@surf4nix.com
 
Cheers, worked perfectly !
sjf
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top