Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 3 of 3
  1. #1
    Member dinologic's Avatar
    Join Date
    Jan 2003
    Posts
    54
    Member #
    539
    this is totally rudimentary but it's driving me crazy. All I want to do is sum up the values of a bunch of fields on a form (which can vary in number). Here are the basics:

    Code:
    var total_qty
    var enc_qty
    for(i=0;i<document.forms(0).length;i++)
         {
         if(document.forms(0).elements(i).name.indexOf("rec_qty")!=-1)
              {
              enc_qty=document.forms(0).elements(i).value
              total_qty=total_qty+enc_qty
              alert(total_qty);
              }
         }
    If there are three fields with values of 100, 500, and 300 respectively, I want total_qty = 900 not 100500300.

    I'll worry about bad input later...but this simple thing is killing me.

    Your assistance is greatly appreciated.

  2.  

  3. #2
    Member dinologic's Avatar
    Join Date
    Jan 2003
    Posts
    54
    Member #
    539
    Nevermind...got it. Number() not number()...case-sensitivity gets me everytime.

  4. #3
    Senior Member nocloset's Avatar
    Join Date
    Sep 2003
    Posts
    108
    Member #
    3228
    A trick I use frequently in Javascript when I'm adding numbers and want to make sure they don't concatenate is simply to subtract zero. Since the minus sign doesn't operate on strings, it forces the script to interpret the operation as numerical addition and subtraction.


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 10:39 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2020 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com