Looking for a simple(?) calculation script

Discussion in 'Programming' started by Acer, Feb 23, 2006.

  1. #1
    I'm looking for what I think is probably a simple javascript calculation form for a web page.
    It looks like this:

    ---------------------------
    Item 1 [Fixed Value] [User Input ] [Fixed Value*User Input]
    Item 2 [Fixed Value] [User Input] [Fixed Value*User Input]
    Item 3 [Fixed Value] [User input] [Fixed Value*User Input]

    [Sum of all (Fixed Value*User Input)]
    ----------------------------

    The only variable supplied by the user would be [User input]. Everything else would be fixed or calculated. [User Input] should default to zero. The 'Sum of" line is very important.

    I don't care if it calculates dynamically or if you have to hit a "submit" button, but the calculations need to appear on the same page as the input form so that the user can make changes easily multiple times after seeing the results.

    I have found some spreadsheet and calculator templates, but so far they do either waaay too much or not enough.

    If anyone can point me to a template, specific tutorial, or have some spare code lying about, I'd be forever grateful. I am javascript illiterate, but if I have the guts I can teach myself how to tweak it from there. Thanks!
     
    Acer, Feb 23, 2006 IP
  2. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
  3. Acer

    Acer Well-Known Member

    Messages:
    112
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    133
    #3
    Thanks. That's the general idea, especially the all-on-one-page.

    But rather than checkbox selection, I need the user to be able to input a quantity into a text field, because the quantity can vary quite a bit from item to item.
     
    Acer, Feb 24, 2006 IP
  4. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Basically you would need to change <input type="checkbox" to <input type="text" and use onfocus instead of onclick to call the script when the user enters the data in the text box.

    The basic syntax for adding up the totals will be:
    item1.value=fixedvalue.value * item1.value;
    item2.value=fixedvalue.value * item2.value;
    finaltotal.value = eval(item1.value) + eval(item2.value);
    Code (markup):
    Don't forget to use a hidden variable for fixedvalue.
     
    mad4, Feb 24, 2006 IP
  5. Acer

    Acer Well-Known Member

    Messages:
    112
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    133
    #5
    Thanks, mad4. I'll wrestle with it and see how far I get.
     
    Acer, Feb 24, 2006 IP