SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict SLeon's Avatar
    Join Date
    Sep 2000
    Location
    Northern California
    Posts
    222
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Disclaimer: I'm used to Visual Basic. And for some reason I'm having a hard time making JavaScript do what I want. Bleah So please please don't think I'm a total idiot, I'm just new at this...

    Anyway, I'm trying to do calculations with JS. Actually, the script already works, but I had to kludge it together, and I would rather use variables to make it streamlined and pretty.

    The thing I'm doing is at http://www.informationsavant.com/gui...enerator.shtml

    What I'm doing is taking the values of three text boxes in a form and, onChange, multiplying them to set the value of a 4th text box. And there are 24 lines like this altogether, like in a spreadsheet. At the moment I have 24 separate functions for doing this, so this is where I'd like to use variables.

    This is example code from one of the changeable text boxes that calls the function:
    <input type="text" name="item01" value="0" size="3" onChange="subtotal01(this.form)")">

    and here is the function:
    function subtotal01 (form) {
    form.subtotal01text.value = form.item01.value * form.time01.value * form.rate01.value;
    }

    When I tried changing the function so that it would take all 4 values in the form of variables, like this:
    function funcSubtotal (form, item, rate, time, subtotal) {
    form.subtotal.value = form.item.value * form.time.value * form.rate.value;
    }

    and the call to the function changed to:
    <input...etc...onChange="funcSubtotal(this.form, item01, time01, rate01, subtotal01")

    the error I got would tell me that "subtotal is null or not valid" (I think that was it, I'm doing this from memory).

    OK, that was long-winded enough...does anyone have any bright ideas? Thanks much...
    InformationSavant - developing intelligent web business
    StrangePegs.com - collectible cards, games, toys, comics

  2. #2
    SitePoint Zealot jonsteele's Avatar
    Join Date
    Jan 2001
    Location
    Toronto
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yould do something like this:

    function calc(item,rate,time,form){
    form.subtotal.value = item * rate * time;
    }

    then all it like this:

    <form name="sub01">
    <input name="item" value="0" onchange="calc(document.sub01.item.value,document.sub01.rate.value,document.sub01.time.value,'subo1')">
    <input name="rate" value="0" onchange="calc(document.sub01.item.value,document.sub01.rate.value,document.sub01.time.value,'subo1')">
    <input name="time" value="0" onchange="calc(document.sub01.item.value,document.sub01.rate.value,document.sub01.time.value,'subo1')">
    <input name="subtotal">
    </form>

    You'd name your forms anything you want but name the subtotal field "subtotal" in all of them. Then you will change the sub01 in all of the onchange's to the name of the form.

    One thing to know is that when you make a function like

    function functionname(variable)

    You will use "variable" as is in the function, and it's value will be whatever you pass to it.

  3. #3
    I believe you have my stapler. scrubz's Avatar
    Join Date
    Feb 2001
    Location
    Van down by the river
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's an easy solution:

    1) Name your form. <form name="quoteForm">

    2) Use this JavaScript function:
    <script language="JavaScript">
    <!--
    function calculateSubtotal(row) {

    frm = document.quoteForm
    frm.elements['subtotal'+row+'text'].value = frm.elements['item'+row].value * frm.elements['time'+row].value * frm.elements['rate'+row].value;

    }
    // -->
    </script>

    3) Pass the row number to the function:

    <input type="text" name="item01" value="0" size="3" onChange="calculateSubtotal('01')">
    <input type="text" name="time01" value="0" size="3" onChange="calculateSubtotal('01')">
    <input type="text" name="rate01" value="0" size="3" onChange="calculateSubtotal('01')"

    This assumes that all of your inputs will be named itemxx, ratexx, timexx, and subtotalxxtext, where the xx is the row number. I wouldn't even bother with passing the form name to the function, since you're only using one form, and the function itself can set that.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •