Solved

Javascript loop count of form object

Posted on 2014-01-16
3
290 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 27

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

777 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