Solved

Javascript loop count of form object

Posted on 2014-01-16
3
285 Views
Last Modified: 2014-01-16
Hi,

I have some code like this:
<FORM NAME="invoices">
<INPUT TYPE="TEXT" NAME="InvoiceQuantity[]">
<INPUT TYPE="BUTTON" onclick="calc();">

Open in new window


What I need is the Javascript code that will sum the contents (which will always be numeric) of all the InvoiceQuantity fields and display this total in a message box.

Many thanks
0
Comment
Question by:kbit
3 Comments
 
LVL 25

Accepted Solution

by:
Luis Pérez earned 500 total points
ID: 39785239
Try this:

var elements = document.getElementsByName("InvoiceQuantity");
var total = 0;
for(var k = 0; k < elements.length; k++) {
    total += Number(elements[k].value);
}
alert(total);

Open in new window

Hope that helps.
0
 
LVL 26

Expert Comment

by:MacroShadow
ID: 39785248
Maybe something like this:

    function calculateSum() {
 
        var sum = 0;
        //iterate through each textboxes and add the values
        $(".txt").each(function() {
 
            //add only if the value is number
            if(!isNaN(this.value) && this.value.length!=0) {
                sum += parseFloat(this.value);
            }
 
        });

         var myMsgBox=new ActiveXObject("wscript.shell")
         myMsgBox.Popup (sum)

    }

Open in new window

0
 

Author Comment

by:kbit
ID: 39785261
Thanks very much Luis, this adapted version worked perfectly:

var elements = document.getElementsByName("InvoiceQuantity[]");
var total = 0;
for(var k = 0; k < elements.length; k++) {
    total += Number(elements[k].value);
}
alert(total);

Open in new window

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now