calculation within a form

Discussion in 'Javascript' started by elji, Nov 5, 2003.

  1. elji

    elji Guest

    In my form, I have 4 objects that I want to work together:


    <input name="price" type="text" id="price" value="100" size="4">

    <input name="quantity" type="text" id="quantity" value="1" size="2">


    <input name="shipping" type="radio" value="slow">

    <input name="shipping" type="radio" value="fast">


    <input name="total" type="text" id="total" size="8">


    I 'd like that when someone checks the first radiobutton (slow),
    the "total" textfield shows: price x quantity + 5

    and when someone checks the second radiobutton (fast),
    the "total" textfield shows: price x quantity + 10


    Sadly, I don't know javascript, but there's got to be an easy way to do
    that, right?

    Thanks to all who will answer, any link to a page that has something
    similar would also be appreciated.
    elji, Nov 5, 2003
    #1
    1. Advertising

  2. elji

    Lee Guest

    elji said:

    ><input name="price" type="text" id="price" value="100" size="4">
    ><input name="quantity" type="text" id="quantity" value="1" size="2">
    ><input name="shipping" type="radio" value="slow">
    ><input name="shipping" type="radio" value="fast">
    ><input name="total" type="text" id="total" size="8">
    >
    >I 'd like that when someone checks the first radiobutton (slow),
    >the "total" textfield shows: price x quantity + 5
    >
    >and when someone checks the second radiobutton (fast),
    >the "total" textfield shows: price x quantity + 10
    >
    >
    >Sadly, I don't know javascript, but there's got to be an easy way to do
    >that, right?


    Is this for a class?
    It seems too simplistic to be for a real website, and I'd hate
    to think that somebody who doesn't know Javascript would try
    to create a commercial web site. You could open yourself to
    all sorts of legal and financial problems.
    Lee, Nov 5, 2003
    #2
    1. Advertising

  3. elji

    elji Guest

    I would do it with onClick

    <input name="shipping" type="radio" value="slow" onClick="...">



    elji wrote:

    > In my form, I have 4 objects that I want to work together:
    >
    >
    > <input name="price" type="text" id="price" value="100" size="4">
    >
    > <input name="quantity" type="text" id="quantity" value="1" size="2">
    >
    >
    > <input name="shipping" type="radio" value="slow">
    >
    > <input name="shipping" type="radio" value="fast">
    >
    >
    > <input name="total" type="text" id="total" size="8">
    >
    >
    > I 'd like that when someone checks the first radiobutton (slow),
    > the "total" textfield shows: price x quantity + 5
    >
    > and when someone checks the second radiobutton (fast),
    > the "total" textfield shows: price x quantity + 10
    >
    >
    > Sadly, I don't know javascript, but there's got to be an easy way to do
    > that, right?
    >
    > Thanks to all who will answer, any link to a page that has something
    > similar would also be appreciated.
    >
    elji, Nov 5, 2003
    #3
  4. elji

    elji Guest

    Lee wrote:

    > elji said:
    >
    >
    >><input name="price" type="text" id="price" value="100" size="4">
    >><input name="quantity" type="text" id="quantity" value="1" size="2">
    >><input name="shipping" type="radio" value="slow">
    >><input name="shipping" type="radio" value="fast">
    >><input name="total" type="text" id="total" size="8">
    >>
    >>I 'd like that when someone checks the first radiobutton (slow),
    >>the "total" textfield shows: price x quantity + 5
    >>
    >>and when someone checks the second radiobutton (fast),
    >>the "total" textfield shows: price x quantity + 10
    >>
    >>
    >>Sadly, I don't know javascript, but there's got to be an easy way to do
    >>that, right?

    >
    >
    > Is this for a class?
    > It seems too simplistic to be for a real website, and I'd hate
    > to think that somebody who doesn't know Javascript would try
    > to create a commercial web site. You could open yourself to
    > all sorts of legal and financial problems.
    >



    No, that's not for a class, this looks simplistic, but that's only a
    part of a very large form, but I've only shown here where my problem is.

    And yes, I hardly know javascript, but my commerce is not about programming.
    elji, Nov 5, 2003
    #4
  5. Lee wrote:

    > Is this for a class?


    What do you mean by `class'?


    PointedEars
    Thomas 'PointedEars' Lahn, Nov 12, 2003
    #5
  6. elji wrote:

    > <input name="price" type="text" id="price" value="100" size="4">
    > <input name="quantity" type="text" id="quantity" value="1" size="2">
    > <input name="shipping" type="radio" value="slow">
    > <input name="shipping" type="radio" value="fast">
    > <input name="total" type="text" id="total" size="8">
    >
    > I 'd like that when someone checks the first radiobutton (slow),
    > the "total" textfield shows: price x quantity + 5


    function calcTotal(o)
    {
    if (o
    && o.value
    && o.form
    && o.form.elements
    && o.form.elements['price']
    && o.form.elements['quantity']
    && o.form.elements['total'])
    {
    var total =
    o.form.elements['price'].value * o.form.elements['quantity'].value;
    var adds = {slow: 5, fast: 10};
    total += (adds[o.value] ? adds[o.value] : 0);
    o.form.elements['total'].value = total;
    }
    }
    ....
    <input name="shipping" type="radio" value="slow"
    onclick="calcTotal(this)">

    > and when someone checks the second radiobutton (fast),
    > the "total" textfield shows: price x quantity + 10


    <input name="shipping" type="radio" value="fast"
    onclick="calcTotal(this)">

    The calcTotal(...) function decides what to add
    depending on the `value' attribute of the radio
    button. Untested.

    > Sadly, I don't know javascript,


    But you can learn it.

    > but there's got to be an easy way to do that, right?


    Yes, it is.


    HTH

    PointedEars
    Thomas 'PointedEars' Lahn, Nov 12, 2003
    #6
  7. elji

    Fabian Guest

    Thomas 'PointedEars' Lahn hu kiteb:

    > Lee wrote:
    >
    >> Is this for a class?

    >
    > What do you mean by `class'?


    Judging from context, I'd guess class as in a course of study.


    --
    --
    Fabian
    Visit my website often and for long periods!
    http://www.lajzar.co.uk
    Fabian, Nov 12, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sunil

    power calculation in fpga

    sunil, Feb 10, 2004, in forum: VHDL
    Replies:
    2
    Views:
    1,279
    Brendan Cullen
    Feb 11, 2004
  2. Dave Smithz
    Replies:
    3
    Views:
    34,621
    mokker
    Oct 30, 2009
  3. Mike
    Replies:
    6
    Views:
    702
  4. Pete
    Replies:
    6
    Views:
    93
    Dr John Stockton
    Dec 31, 2003
  5. Replies:
    9
    Views:
    131
    Randy Webb
    Jan 25, 2006
Loading...

Share This Page