Closed Thread Icon

Preserved Topic: trying to add values in a text field (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=18471" title="Pages that link to Preserved Topic: trying to add values in a text field (Page 1 of 1)" rel="nofollow" >Preserved Topic: trying to add values in a text field <span class="small">(Page 1 of 1)</span>\

 
TheDude
Nervous Wreck (II) Inmate

From: Cincinnati, OH
Insane since: Feb 2002

posted posted 02-12-2002 06:04

I'm trying to add values from text fields and put the total in another field. I've tried converting them to integers using parseInt, but no matter what I try, it just does text adds (e.g. 2+2=22 instead of 4).
Here is the function I'm using (called by onChange):

function add()
{
var num1=window.document._form.test.value;
var num2=window.document._form.test2.value;
var total=num1+num2;
window.document._form.test2.value=total;
}

What is a good way to do totals?
I can't find it in tutorials and references I've looked in.
Thanks.

TheDude

Bugimus
Maniac (V) Mad Scientist

From: New California
Insane since: Mar 2000

posted posted 02-12-2002 06:40

TheDude,

You were correct in using parseInt. This should work:

function add()
{
var num1=parseInt(window.document._form.test.value);
var num2=parseInt(window.document._form.test2.value);
var total=num1+num2;
window.document._form.test2.value=total;
}

Example here: http://bugimus.com/gurus/thedude/inputfield_adds.html



[This message has been edited by Bugimus (edited 02-12-2002).]

TheDude
Nervous Wreck (II) Inmate

From: Cincinnati, OH
Insane since: Feb 2002

posted posted 02-12-2002 18:34

Thanks!
I got it working after seeing your code.

TheDude

Bugimus
Maniac (V) Mad Scientist

From: New California
Insane since: Mar 2000

posted posted 02-12-2002 19:49



Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 02-12-2002 21:57

Also consider parseFloat if you want to allow decimals.

TheDude
Nervous Wreck (II) Inmate

From: Cincinnati, OH
Insane since: Feb 2002

posted posted 02-14-2002 20:31

Thanks Slime! Turns out, I do have a decimal so I needed to know that function.

How do I format a text field? e.g. I get a result of 4.84000 when I only want two decimal places.

This is my first thread on the Asylum, so, by way of introduction: I am a (deranged) IT professional, fairly new to Javascript; I've gone through the MindLeaders.com Javascript tutorial, and almost finished with the one on Webmonkey.
Later, I'll be good enough to contribute instead of just ask stuff!
I'm also a friend of Pugzly, (since 9th grade). I hope that doesn't undermine my status here!

TheDude

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 02-14-2002 21:20

Hmm, if you *really* want to learn JavaScript, well, then get a book - I recommend JavaScript: The Definitive Guide from O'Reilly ( www.oreilly.com ).

To round a number to x decimal places:

1. multiply the number by 10^x
2. use Math.round() to round it off
3. divide the number by 10^x

For instance:

roundedtotwoplaces = Math.round(number*100)/100;

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 02-15-2002 05:30

I swear I've never met the guy.....

« BackwardsOnwards »

Show Forum Drop Down Menu