Solved

Javascript loop count of form object

Posted on 2014-01-16
3
295 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…
Suggested Courses

623 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