Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-19-2006, 04:58 PM   PM User | #1
JohnnyV
New Coder

 
Join Date: Sep 2005
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
JohnnyV is an unknown quantity at this point
Return sum of all input values within loop

OK, so I have a bunch of input fields, each which will have a numerical value. There can be any number of input fields in the form, and that number will vary depending on what the user has selected prior in the application.

So basically I created a loop that goes though all of the INPUT fields within the form. How can I have it find the value of each field and add them all together, then return to me the total? It'll need to be made into a variable, but a simple alert with the total value will give me enough to work with.

Any ideas?
JohnnyV is offline   Reply With Quote
Old 01-19-2006, 05:30 PM   PM User | #2
A1ien51
Senior Coder

 
A1ien51's Avatar
 
Join Date: Jun 2002
Location: Between DC and Baltimore In a Cave
Posts: 2,717
Thanks: 1
Thanked 94 Times in 88 Posts
A1ien51 will become famous soon enough
Basic idea:

Code:
var aForm = document.formName;
for(var i=0;i<aForm.elements.length;i++){
  if(aForm.elements[i].type=="text")alert(aForm.elements[i].value);
}
Eric
__________________
Tech Author [Ajax In Action, JavaScript: Visual Blueprint]
A1ien51 is offline   Reply With Quote
Old 01-19-2006, 05:31 PM   PM User | #3
vwphillips
Senior Coder

 
Join Date: Mar 2005
Location: Portsmouth UK
Posts: 4,382
Thanks: 3
Thanked 466 Times in 453 Posts
vwphillips is a jewel in the roughvwphillips is a jewel in the roughvwphillips is a jewel in the rough
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
  <title></title>
<script language="JavaScript" type="text/javascript">
<!--
var TotalValue;
function Total(obj,cls){
 TotalValue=0;
 var els=obj.elements;
 for (var zxc0=0;zxc0<els.length;zxc0++){
  if (els[zxc0].className==cls){
   els[zxc0].value=els[zxc0].value.replace(/\D/g,'');
   if (els[zxc0].value.length>0){
    TotalValue+=parseInt(els[zxc0].value);
   }
  }
 }
 document.getElementById('Result').value=TotalValue;
}
//-->
</script></head>

<body>
<form  onkeyup="Total(this,'total');">
<input class="total" >
<input class="total" >
<input class="total" >
<input class="total" >
<input class="total" >
<input class="total" >
<input id="Result" onkeyup="return false;" value="0">
</form>
</body>

</html>
vwphillips is online now   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:44 AM.


Advertisement
Log in to turn off these ads.