Solved

Javascript loop count of form object

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

729 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