Calculate field values using javascript

M

mehere

Please excuse me if I am posting in wrong NG.

Can someone explain how I can use javascript (I am assuming this is the best
way to do it) to add two field values together. I.e. field 1 plus field 2
equals field 3 (this is in a php form)

Also how can I set a field to be a value depending on what the user selects,
i.e. they may have eight items to select from and each one has a different
value.

The above javascript will then use that value to add to another field value
to give a me a total value

I know this should be simple and I am looking around and reading how to do
this, but any pointers or sharable code would be helpful.

Cheers

Greg
 
H

Hal Rosser

mehere said:
Please excuse me if I am posting in wrong NG.

Can someone explain how I can use javascript (I am assuming this is the best
way to do it) to add two field values together. I.e. field 1 plus field 2
equals field 3 (this is in a php form)

Also how can I set a field to be a value depending on what the user selects,
i.e. they may have eight items to select from and each one has a different
value.

The above javascript will then use that value to add to another field value
to give a me a total value

I know this should be simple and I am looking around and reading how to do
this, but any pointers or sharable code would be helpful.

There are many examples at this website:
http://www.w3schools.com/js/js_examples.asp
some folks say the site has many errors - maybe so - but it also has many
good examples.
Take a look and decide for yourself.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated
Sat, 25 Feb 2006 23:27:39 remote, seen in Hal
Rosser said:
There are many examples at this website:
http://www.w3schools.com/js/js_examples.asp
some folks say the site has many errors - maybe so - but it also has many
good examples.
Take a look and decide for yourself.

If the OP knows as little as he asserts, he will not be able (with
reasonable effort) to judge which parts, if any, are good.


Actually, there are two ways of adding; if one adds "cat" and "fish" one
gets "catfish", but if one adds 1 and 2 one gets 3. Javascript supports
both of those, alas with the same operator.

Generally, one wants to do more than the OP asks; hence a slightly more
general solution will be more informative.

Let ADDR stand for some way of addressing a field.

Then ADDR.value is, for the sort of control in question, a String
holding the content of the field. If that string represents a number,
then +ADDR.value gives a Number of appropriate value.

So: var Qty1 = +ADDR1.value
var Qty2 = +ADDR2.value
ADDR3.value = Qty1 + Qty2 // auto-converted to String

For the second case, use selectedIndex to address the field chosen, like
PlaceValue.value = + Places.options[Places.selectedIndex].value

Read the newsgroup FAQ.
 
J

Jonas Raoni

Dr said:
So: var Qty1 = +ADDR1.value
var Qty2 = +ADDR2.value
ADDR3.value = Qty1 + Qty2 // auto-converted to String

I preffer:

+field.value || 0

Then, NaN values will be converted to 0
 
M

mehere

Dr John Stockton said:
JRS: In article <[email protected]>, dated
Sat, 25 Feb 2006 23:27:39 remote, seen in Hal
Rosser said:
There are many examples at this website:
http://www.w3schools.com/js/js_examples.asp
some folks say the site has many errors - maybe so - but it also has many
good examples.
Take a look and decide for yourself.

If the OP knows as little as he asserts, he will not be able (with
reasonable effort) to judge which parts, if any, are good.


Actually, there are two ways of adding; if one adds "cat" and "fish" one
gets "catfish", but if one adds 1 and 2 one gets 3. Javascript supports
both of those, alas with the same operator.

Generally, one wants to do more than the OP asks; hence a slightly more
general solution will be more informative.

Let ADDR stand for some way of addressing a field.

Then ADDR.value is, for the sort of control in question, a String
holding the content of the field. If that string represents a number,
then +ADDR.value gives a Number of appropriate value.

So: var Qty1 = +ADDR1.value
var Qty2 = +ADDR2.value
ADDR3.value = Qty1 + Qty2 // auto-converted to String

For the second case, use selectedIndex to address the field chosen, like
PlaceValue.value = + Places.options[Places.selectedIndex].value

Read the newsgroup FAQ.

OP here

Thanks and yes VERY new to this cheers for your help

Greg
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Tue, 28 Feb 2006
05:10:57 remote, seen in Jonas Raoni
I preffer:

+field.value || 0

Then, NaN values will be converted to 0

In general, NaN values should not be converted to zero; they should
result in a form of error message suited to the application.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top