Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Javascript loop count of form object

Posted on 2014-01-16
3
Medium Priority
?
296 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…

688 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