#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2001
    Posts
    8
    Rep Power
    0

    Adding Variables in Javascript


    I created a function to add values that are entered into text boxes and then writes those added values to a text box in the parent document. Instead of adding the values it concatenates them and writes the concatenated numbers in the parent document. What am I doing wrong? Here is the function code:

    var addTotal = 0
    function updateParent(partAmt){
    var subTotal = 0

    subTotal = partAmt.value
    addTotal = addTotal+subTotal
    opener.document.transact.tot_pay.value = addTotal
    }

    The 3 text boxes are coded like this:

    <input type="text" name="cc_amount" onBlur="updateParent(this)">
    <input type="text" name="ck_amount" onBlur="updateParent(this)">
    <input type="text" name="o_amount" onBlur="updateParent(this)">
  2. #2
  3. No Profile Picture
    Senior Citizen
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2001
    Location
    leftcoast
    Posts
    2,019
    Rep Power
    16
    Form values are strings; adding strings concatenates them. Try this:

    var addTotal = 0;
    function updateParent(partAmt) {
    addTotal += partAmt;
    opener.document.transact.tot_pay.value = addTotal;
    }

    <input type="text" name="cc_amount" onBlur="updateParent(eval(this.value))">

    Might as well pass the number (eval()ed field value) since that's what your function needs.

IMN logo majestic logo threadwatch logo seochat tools logo