Forum Moderators: open

Message Too Old, No Replies

javascript calculations

         

aweise

5:04 pm on Apr 30, 2004 (gmt 0)

10+ Year Member



How would I go about having three fields next to each other. The first one for adding quantity, the second for adding cost and then finally the third would show the result of the first two. When there are no values this field will show a zero.

How would I create this?

moltar

5:43 pm on Apr 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



  1. Create a form (
    calc
    ) with three input elements next to each other (
    qty, cost, total
    )
  2. Write a function that will read data from
    qty
    and
    cost
    inputs
  3. Add up the values
  4. Write total to
    total
    .
  5. Set
    onchange
    action calling the above function on qty and cost fields.
  6. Optional: add a button (
    type="button"
    ) in the form and put
    onclick
    action on it calling the above function

You can get/set value of a field like this:

document.calc.[b]FIELD_NAME[/b].value

aweise

5:49 pm on Apr 30, 2004 (gmt 0)

10+ Year Member



I'm a bit clueless on writing javascript functions. But I will try to go online and search this out given the steps you have outlined.

Thanks for your help.

moltar

5:55 pm on Apr 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Functions are pretty much like in other languages:

function calc() {
... does calculations ...
}