Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. javascript calculation total not working

javascript calculation total not working

Scheduled Pinned Locked Moved Web Development
javascripthtmlcomsalestools
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    esebagel
    wrote on last edited by
    #1

    Hi there, I really hope you can help I have worked on this all day today and last night. I can't get the Total box to calculate. I am trying to create an online invoice for my customer to print from my web page. I have scoured the internet looking for some help, but it doesn't help that I am clueless about javascript. This is what I have tried: In my table, all I want to happen is for the user to type in a price in the Amount boxes and then have a total at the bottom. Seems pretty basic, but I just don't understand why it won't work (not really sure of what I am doing) Can somebody please help me? The form itself can be found at www.ahtcsonline.com/invoice.html - the source is too much to copy so it may be easier to view the form and then view the source code underneath it. Thank you in advance, Evan <script type="text/javascript"> function totalamountdue(){ { var amount, amount2, amount3, amount4, amount5, amount6, amount7, amount8, amount9, amount10; Amount = document.Invamt.Amount.value; Amount2 = document.Invamt.Amount2.value; Amount3 = document.Invamt.Amount3.value; Amount4 = document.Invamt.Amount4.value; Amount5 = document.Invamt.Amount5.value; Amount6 = document.Invamt.Amount6.value; Amount7 = document.Invamt.Amount7.value; Amount8 = document.Invamt.Amount8.value; Amount9 = document.Invamt.Amount9.value; Amount10 = document.Invamt.Amount10.value; Total = document.Invamt.Total.value; document.Invamt.Total.value = (amount + amount2 + amount3 + amount4 + amount5 + amount6 + amount7 + amount8 + amount9 + amount10); } } </script>

    J D B 3 Replies Last reply
    0
    • E esebagel

      Hi there, I really hope you can help I have worked on this all day today and last night. I can't get the Total box to calculate. I am trying to create an online invoice for my customer to print from my web page. I have scoured the internet looking for some help, but it doesn't help that I am clueless about javascript. This is what I have tried: In my table, all I want to happen is for the user to type in a price in the Amount boxes and then have a total at the bottom. Seems pretty basic, but I just don't understand why it won't work (not really sure of what I am doing) Can somebody please help me? The form itself can be found at www.ahtcsonline.com/invoice.html - the source is too much to copy so it may be easier to view the form and then view the source code underneath it. Thank you in advance, Evan <script type="text/javascript"> function totalamountdue(){ { var amount, amount2, amount3, amount4, amount5, amount6, amount7, amount8, amount9, amount10; Amount = document.Invamt.Amount.value; Amount2 = document.Invamt.Amount2.value; Amount3 = document.Invamt.Amount3.value; Amount4 = document.Invamt.Amount4.value; Amount5 = document.Invamt.Amount5.value; Amount6 = document.Invamt.Amount6.value; Amount7 = document.Invamt.Amount7.value; Amount8 = document.Invamt.Amount8.value; Amount9 = document.Invamt.Amount9.value; Amount10 = document.Invamt.Amount10.value; Total = document.Invamt.Total.value; document.Invamt.Total.value = (amount + amount2 + amount3 + amount4 + amount5 + amount6 + amount7 + amount8 + amount9 + amount10); } } </script>

      J Offline
      J Offline
      Jason Vetter
      wrote on last edited by
      #2

      Try this: function totalamountdue(){ var Amount = document.getElementById("Amount").value; var Amount2 = document.getElementById("Amount2").value; var Amount3 = document.getElementById("Amount3").value; var Amount4 = document.getElementById("Amount4").value; var Amount5 = document.getElementById("Amount5").value; var Amount6 = document.getElementById("Amount6").value; var Amount7 = document.getElementById("Amount7").value; var Amount8 = document.getElementById("Amount8").value; var Amount9 = document.getElementById("Amount9").value; var Amount10 = document.getElementById("Amount10").value; document.getElementById("Total").value = amount + amount2 + amount3 + amount4 + amount5 + amount6 + amount7 + amount8 + amount9 + amount10; } We really should add some error handling and/or only allow numbers in the text boxes.

      E 1 Reply Last reply
      0
      • J Jason Vetter

        Try this: function totalamountdue(){ var Amount = document.getElementById("Amount").value; var Amount2 = document.getElementById("Amount2").value; var Amount3 = document.getElementById("Amount3").value; var Amount4 = document.getElementById("Amount4").value; var Amount5 = document.getElementById("Amount5").value; var Amount6 = document.getElementById("Amount6").value; var Amount7 = document.getElementById("Amount7").value; var Amount8 = document.getElementById("Amount8").value; var Amount9 = document.getElementById("Amount9").value; var Amount10 = document.getElementById("Amount10").value; document.getElementById("Total").value = amount + amount2 + amount3 + amount4 + amount5 + amount6 + amount7 + amount8 + amount9 + amount10; } We really should add some error handling and/or only allow numbers in the text boxes.

        E Offline
        E Offline
        esebagel
        wrote on last edited by
        #3

        Hi There Jason, I wished it worked, I was really hoping but nothing calculated when I hit the "calculate" button. I want you to also know that I have tried so many iterations before I asked for help. I am very tenacious and still looked on the net after my submission. Wish I knew what I was doing but I truly think that the subject is over my head. Although I did create my website from scratch back in August and I knew nothing of HTML...not that I know that much now ;) Thank you for trying, Evan

        1 Reply Last reply
        0
        • E esebagel

          Hi there, I really hope you can help I have worked on this all day today and last night. I can't get the Total box to calculate. I am trying to create an online invoice for my customer to print from my web page. I have scoured the internet looking for some help, but it doesn't help that I am clueless about javascript. This is what I have tried: In my table, all I want to happen is for the user to type in a price in the Amount boxes and then have a total at the bottom. Seems pretty basic, but I just don't understand why it won't work (not really sure of what I am doing) Can somebody please help me? The form itself can be found at www.ahtcsonline.com/invoice.html - the source is too much to copy so it may be easier to view the form and then view the source code underneath it. Thank you in advance, Evan <script type="text/javascript"> function totalamountdue(){ { var amount, amount2, amount3, amount4, amount5, amount6, amount7, amount8, amount9, amount10; Amount = document.Invamt.Amount.value; Amount2 = document.Invamt.Amount2.value; Amount3 = document.Invamt.Amount3.value; Amount4 = document.Invamt.Amount4.value; Amount5 = document.Invamt.Amount5.value; Amount6 = document.Invamt.Amount6.value; Amount7 = document.Invamt.Amount7.value; Amount8 = document.Invamt.Amount8.value; Amount9 = document.Invamt.Amount9.value; Amount10 = document.Invamt.Amount10.value; Total = document.Invamt.Total.value; document.Invamt.Total.value = (amount + amount2 + amount3 + amount4 + amount5 + amount6 + amount7 + amount8 + amount9 + amount10); } } </script>

          D Offline
          D Offline
          daveyerwin
          wrote on last edited by
          #4

          function totalamountdue() {
          var inputs = document.getElementsByTagName("INPUT");
          var runningTotal = 0;
          for (i = 0; i < inputs.length; i++) {
          if (inputs[i].id == "total") {
          inputs[i].value = runningTotal / 100;
          var a = inputs[i].value.split(".");
          if (!a[1]) {inputs[i].value += ".00"}
          else if (a[1].indexOf("0") != 0 && a[1] < 10) { inputs[i].value += "0" }
          }
          if (inputs[i].id.indexOf("Amount") == 0) {
          if (/^[1-9][0-9]{0,2}(,[0-9]{3})*(\.[0-9]{2})?$/.test(inputs[i].value)) {
          runningTotal += +inputs[i].value * 100;
          } else {
          if (inputs[i].value == "THIS IS A BAD VALUE") { inputs[i].value = ""; }
          if (inputs[i].value) { inputs[i].value = "THIS IS A BAD VALUE"; }
          }
          }

                  }
              } <>
          

          -- Modified Friday, February 26, 2010 1:35 AM

          E D 2 Replies Last reply
          0
          • D daveyerwin

            function totalamountdue() {
            var inputs = document.getElementsByTagName("INPUT");
            var runningTotal = 0;
            for (i = 0; i < inputs.length; i++) {
            if (inputs[i].id == "total") {
            inputs[i].value = runningTotal / 100;
            var a = inputs[i].value.split(".");
            if (!a[1]) {inputs[i].value += ".00"}
            else if (a[1].indexOf("0") != 0 && a[1] < 10) { inputs[i].value += "0" }
            }
            if (inputs[i].id.indexOf("Amount") == 0) {
            if (/^[1-9][0-9]{0,2}(,[0-9]{3})*(\.[0-9]{2})?$/.test(inputs[i].value)) {
            runningTotal += +inputs[i].value * 100;
            } else {
            if (inputs[i].value == "THIS IS A BAD VALUE") { inputs[i].value = ""; }
            if (inputs[i].value) { inputs[i].value = "THIS IS A BAD VALUE"; }
            }
            }

                    }
                } <>
            

            -- Modified Friday, February 26, 2010 1:35 AM

            E Offline
            E Offline
            esebagel
            wrote on last edited by
            #5

            Dave, I could hug you...or just give you five! Thank you and the previous person, Jason for all of your efforts. I hate to ask for help, but there was no way I was going to get this one. Sometimes I come up with these ambitious ideas but with no way to implement them. Thank you very much, Evan Einhorn

            D 1 Reply Last reply
            0
            • E esebagel

              Dave, I could hug you...or just give you five! Thank you and the previous person, Jason for all of your efforts. I hate to ask for help, but there was no way I was going to get this one. Sometimes I come up with these ambitious ideas but with no way to implement them. Thank you very much, Evan Einhorn

              D Offline
              D Offline
              daveyerwin
              wrote on last edited by
              #6

              stay tenacious

              1 Reply Last reply
              0
              • D daveyerwin

                function totalamountdue() {
                var inputs = document.getElementsByTagName("INPUT");
                var runningTotal = 0;
                for (i = 0; i < inputs.length; i++) {
                if (inputs[i].id == "total") {
                inputs[i].value = runningTotal / 100;
                var a = inputs[i].value.split(".");
                if (!a[1]) {inputs[i].value += ".00"}
                else if (a[1].indexOf("0") != 0 && a[1] < 10) { inputs[i].value += "0" }
                }
                if (inputs[i].id.indexOf("Amount") == 0) {
                if (/^[1-9][0-9]{0,2}(,[0-9]{3})*(\.[0-9]{2})?$/.test(inputs[i].value)) {
                runningTotal += +inputs[i].value * 100;
                } else {
                if (inputs[i].value == "THIS IS A BAD VALUE") { inputs[i].value = ""; }
                if (inputs[i].value) { inputs[i].value = "THIS IS A BAD VALUE"; }
                }
                }

                        }
                    } <>
                

                -- Modified Friday, February 26, 2010 1:35 AM

                D Offline
                D Offline
                daveyerwin
                wrote on last edited by
                #7

                function totalamountdue() { var inputs = document.getElementsByTagName("INPUT"); var runningTotal = 0; for (i = 0; i < inputs.length; i++) { if (inputs[i].id == "total") { inputs[i].value = runningTotal / 100; var a = inputs[i].value.split("."); if (!a[1]) {inputs[i].value += ".00"} else if (a[1].indexOf("0") != 0 && a[1] < 10) { inputs[i].value += "0" } } if (inputs[i].id.indexOf("Amount") == 0) { if (/^[1-9][0-9]{0,2}(,[0-9]{3})*(\.[0-9]{2})?$/.test(inputs[i].value)) { runningTotal += +inputs[i].value * 100; } else { if (inputs[i].value == "THIS IS A BAD VALUE") { inputs[i].value = ""; } if (inputs[i].value) { inputs[i].value = "THIS IS A BAD VALUE"; } } } } }

                1 Reply Last reply
                0
                • E esebagel

                  Hi there, I really hope you can help I have worked on this all day today and last night. I can't get the Total box to calculate. I am trying to create an online invoice for my customer to print from my web page. I have scoured the internet looking for some help, but it doesn't help that I am clueless about javascript. This is what I have tried: In my table, all I want to happen is for the user to type in a price in the Amount boxes and then have a total at the bottom. Seems pretty basic, but I just don't understand why it won't work (not really sure of what I am doing) Can somebody please help me? The form itself can be found at www.ahtcsonline.com/invoice.html - the source is too much to copy so it may be easier to view the form and then view the source code underneath it. Thank you in advance, Evan <script type="text/javascript"> function totalamountdue(){ { var amount, amount2, amount3, amount4, amount5, amount6, amount7, amount8, amount9, amount10; Amount = document.Invamt.Amount.value; Amount2 = document.Invamt.Amount2.value; Amount3 = document.Invamt.Amount3.value; Amount4 = document.Invamt.Amount4.value; Amount5 = document.Invamt.Amount5.value; Amount6 = document.Invamt.Amount6.value; Amount7 = document.Invamt.Amount7.value; Amount8 = document.Invamt.Amount8.value; Amount9 = document.Invamt.Amount9.value; Amount10 = document.Invamt.Amount10.value; Total = document.Invamt.Total.value; document.Invamt.Total.value = (amount + amount2 + amount3 + amount4 + amount5 + amount6 + amount7 + amount8 + amount9 + amount10); } } </script>

                  B Offline
                  B Offline
                  Boro_Bob
                  wrote on last edited by
                  #8

                  I know you have already been given a solution to this, but I just thought I give you an important tip. This didn't work because you are declaring and adding variables with a lowercase 'a' in amount, but assigning the values to an uppercase Amount. This makes them different variables. Amount != amount. JavaScript is case sensitive.

                  Words fade as the meanings change, but somehow, it don't bother me.

                  E 1 Reply Last reply
                  0
                  • B Boro_Bob

                    I know you have already been given a solution to this, but I just thought I give you an important tip. This didn't work because you are declaring and adding variables with a lowercase 'a' in amount, but assigning the values to an uppercase Amount. This makes them different variables. Amount != amount. JavaScript is case sensitive.

                    Words fade as the meanings change, but somehow, it don't bother me.

                    E Offline
                    E Offline
                    esebagel
                    wrote on last edited by
                    #9

                    Hi Boro_Bob, I really appreciate the information. Last August I lost my job and then decided that I would do my own thing. I knew I needed a website so I started working on it. I knew absolutely nothing about HTML. Little by little I have learned along the way and I borrowed lots of code I found on the net but only employed it if I could get it to work. Instructions varied so if it worked great if not I moved on. Javascript...not a clue. But again I scoured the internet to make my column add up and tried many different iterations. I should have known what you said to be true about the code being case sensitive...as it is true for HTML. During the course of building my website I had decided to capitalize the first letter of the file names when I was done editing so I could keep track of what was done. Lesson learned, never capitalize the "I" in index.html or it will give you a 404 Error code and the Home page won't work.LOL. My web page has been about scope creep, every time I get something to work I then think of something to make it better. For instance, the columns adding up now works great...my next idea was to have drop downs with the 3 or so varying amounts I charge for my online classes. You know I had to try it but since I don't know squat about JS I gave up my idea. Again I want to say "thank you" for explaining that JS was case sensitive. I tried like hell to figure it out on my own and that never dawned on me. Evan Einhorn www.AHTCSonline.com

                    B 1 Reply Last reply
                    0
                    • E esebagel

                      Hi Boro_Bob, I really appreciate the information. Last August I lost my job and then decided that I would do my own thing. I knew I needed a website so I started working on it. I knew absolutely nothing about HTML. Little by little I have learned along the way and I borrowed lots of code I found on the net but only employed it if I could get it to work. Instructions varied so if it worked great if not I moved on. Javascript...not a clue. But again I scoured the internet to make my column add up and tried many different iterations. I should have known what you said to be true about the code being case sensitive...as it is true for HTML. During the course of building my website I had decided to capitalize the first letter of the file names when I was done editing so I could keep track of what was done. Lesson learned, never capitalize the "I" in index.html or it will give you a 404 Error code and the Home page won't work.LOL. My web page has been about scope creep, every time I get something to work I then think of something to make it better. For instance, the columns adding up now works great...my next idea was to have drop downs with the 3 or so varying amounts I charge for my online classes. You know I had to try it but since I don't know squat about JS I gave up my idea. Again I want to say "thank you" for explaining that JS was case sensitive. I tried like hell to figure it out on my own and that never dawned on me. Evan Einhorn www.AHTCSonline.com

                      B Offline
                      B Offline
                      Boro_Bob
                      wrote on last edited by
                      #10

                      No problem. Good luck with your site.

                      Words fade as the meanings change, but somehow, it don't bother me.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups