Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Help! - Mathmatics with variables

3 views
Skip to first unread message

Rob@

unread,
Aug 30, 2004, 6:20:03 AM8/30/04
to
Hi, i am trying to add two values together from two text fields and place
them in another text field, i know a little javascript and have got the
basics working, only i cant't get the values to add together, they just
display as the two values side by side.

Any help appreciated

Rob


Art

unread,
Aug 31, 2004, 5:30:42 PM8/31/04
to
First of all I'm on WebTV, and the book I'm reading is four years old,
so I have no way of knowing if this is still compatible with the current
PC browsers. I too am in the learning process. Hope this will work.

As David Thau explains in his book, it might have something to do with
IE treating the content of form elements as strings, even when they're
numbers so it concatenates the numbers instead of adding them. He
suggests using parseInt() and I think that parseFloat() also works in
this case.

Here's my suggested solution:

http://www.xlectric.com/temp/addtest.html

If you come back to this group and see this, please let me know if it
works.

Later, Art.

http://vmdd.tech.mylinuxisp.com/catalog/

unread,
Sep 9, 2004, 1:00:51 AM9/9/04
to
To induce javascript to do math instead of string concatenation,
substracting 0 from each of the field. For example:

(field1 - 0) + (field2 - 0)

VMDD Tech
http://vmdd.tech.mylinuxisp.com/catalog/

"Rob@" <rob@rob<REMOVETHIS>sweb.karoo.co.uk> wrote in message news:<nimdnbt0Buc...@karoo.co.uk>...

Evertjan.

unread,
Sep 9, 2004, 4:15:07 AM9/9/04
to
http://vmdd.tech.mylinuxisp.com/catalog/ wrote on 09 sep 2004 in
comp.lang.java.javascript:
> "Rob@" <rob@rob<REMOVETHIS>sweb.karoo.co.uk> wrote in message
> news:<nimdnbt0Buc...@karoo.co.uk>...
>> Hi, i am trying to add two values together from two text fields and
>> place them in another text field, i know a little javascript and have
>> got the basics working, only i cant't get the values to add together,
>> they just display as the two values side by side.
>
> To induce javascript to do math instead of string concatenation,
> substracting 0 from each of the field. For example:
>
> (field1 - 0) + (field2 - 0)

Why not simply use the unary + ?

numericResult = (+field1 + +field2)

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress,
but let us keep the discussions in the newsgroup)

Art

unread,
Sep 9, 2004, 10:54:00 AM9/9/04
to
For what it's worth,

After I posted my reply I tried it out myself. Turns out that parseInt()
doesn't handle the decimals so I use parseFloat() instead.

Later, Art.

0 new messages