1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Values adding up wrong with Javascript

Discussion in 'Software Development' started by smooth, Oct 31, 2005.

Thread Status:
Not open for further replies.
Advertisement
  1. smooth

    smooth Thread Starter

    Joined:
    Sep 26, 2005
    Messages:
    4,029
    First Name:
    Garrett
    Hello again,

    I have a form on a web page. There are text boxes down a column, and at the bottom there is a text box that I want to have the total of the textboxes above it. I have the initial value of every text box set to 0. I have this javascript in a external file, and have set it to the onChange even of each of the text boxes:

    Code:
    function fillAppsTotal()
    	{
    		var appsTotalValue
    		var americoAppsValue = document.weeklyProduction.americoApps.value
    		var chaseAppsValue = document.weeklyProduction.chaseApps.value
    		var chesapeakeAppsValue = document.weeklyProduction.chesapeakeApps.value
    		var coloradoAppsValue = document.weeklyProduction.coloradoApps.value
    		var fgAppsValue = document.weeklyProduction.fgApps.value
    		var illinoisAppsValue = document.weeklyProduction.illinoisApps.value
    		var lifeInvestorsAppsValue = document.weeklyProduction.lifeInvestorsApps.value
    		var lincolnAppsValue = document.weeklyProduction.lincolnApps.value
    		var mutualAppsValue = document.weeklyProduction.mutualApps.value
    		var presidentialAppsValue = document.weeklyProduction.presidentialApps.value
    		var shenandoahAppsValue = document.weeklyProduction.shenandoahApps.value
    		var usFinancialAppsValue = document.weeklyProduction.usFinancialApps.value
    		
    		appsTotalValue = americoAppsValue + chaseAppsValue + chesapeakeAppsValue + coloradoAppsValue 
    		+ fgAppsValue + illinoisAppsValue + lifeInvestorsAppsValue + lincolnAppsValue + mutualAppsValue
    		+ presidentialAppsValue + shenandoahAppsValue + usFinancialAppsValue
    		
    		document.weeklyProduction.appsTotal.value = appsTotalValue
    	}
    Not sure if it is the most efficient way of doing this. However, my problem is, instead of adding up the values in the text box, it is just putting the numbers beside each other in the order of the text boxes.

    For example, say I have this right here down the line
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12

    Instead of giving me the total, 78, it gives me 123456789101112.

    Any ideas on why this is happening, and what I can do to fix this?

    Thanks.
     
  2. maro25

    maro25

    Joined:
    Oct 14, 2005
    Messages:
    25
    Well you are adding strings instead of numbers, you should convert all values to ints to be able to add them
     
  3. smooth

    smooth Thread Starter

    Joined:
    Sep 26, 2005
    Messages:
    4,029
    First Name:
    Garrett
    Thanks, I really appreciate it. That did the trick, I looked up how to convert, and converted them using parseFloat, since I'll have decimals as well. :) Thanks again. :D
     
  4. maro25

    maro25

    Joined:
    Oct 14, 2005
    Messages:
    25
    you're welcome, glad it worked :)
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/412729

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice