Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Loop and sum

Reply
Thread Tools

Loop and sum

 
 
staeri@gmail.com
Guest
Posts: n/a
 
      04-18-2006
I have an unknown number of textboxes which I want to sum. They are
named something like this:

"ctl00$ContentPlaceHolder1$DataList1$ctl03$DataLis t2$ctl03$txtSumB"

The only thing i know for sure is that "txtSumB" is somewhere in the
name. The page can contain anything from 1 to 50 textboxes.

Can someone please help me with a script which loops through all the
textboxes with "txtSumB" in the name and sums the values?

Very grateful for help!

Regards,

S

 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      04-18-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I have an unknown number of textboxes which I want to sum. They are
> named something like this:
>
> "ctl00$ContentPlaceHolder1$DataList1$ctl03$DataLis t2$ctl03$txtSumB"
>
> The only thing i know for sure is that "txtSumB" is somewhere in the
> name. The page can contain anything from 1 to 50 textboxes.
>
> Can someone please help me with a script which loops through all the
> textboxes with "txtSumB" in the name and sums the values?


Get the elements within the form, find those with a name that has the
string in it and add them up.

<script type="text/javascript">

function showTotal(form, totalEl)
{
var el, els = form.elements;
var sum = 0;

for (var i=0, num=els.length; i<num; ++i){
el = els[i];

if ('text' == el.type && /SumB/.test(el.name)){
sum += +el.value;
}
form.elements[totalEl].value = sum;
}
}
</script>

<form action=""><div>
<input type="text" name="ZugZug">ZugZug<br>
<input type="text" name="fooSumBxx">fooSumBxx<br>
<input type="text" name="barSumByy">barSumByy<br>
<input type="text" name="sueSumBzz">sueSumBzz<br>
<input type="text" name="formTotal" readonly>Total
<input type="button" value="Show total"
onclick="showTotal(this.form, 'formTotal');">
</div></form>


Note that you need to test the value of each input to see if it's a
number and suitable to be added before actually adding it.


--
Rob
 
Reply With Quote
 
 
 
 
staeri@gmail.com
Guest
Posts: n/a
 
      04-18-2006
Thank's a lot!

 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Count bits in VHDL, with loop and unrolled loop produces different results a s VHDL 16 03-08-2011 05:35 PM
Re: Count bits in VHDL, with loop and unrolled loop producesdifferent results a s VHDL 2 03-04-2011 08:08 PM
IOError and Try Again to loop the loop. The Danny Bos Python 7 07-12-2010 04:34 AM
Whats the difference between while loop in Windows message loop and while(1) Uday Bidkar C++ 4 12-12-2006 12:30 PM



Advertisments