Here's a suggestion to help you start:
- Wrap your
Repeater
in a <div>
and assign it a unique id
.
- Utilize the
document.getElementById()
function in JavaScript to get a reference to that <div>
.
- Use the
getElementsByTagName()
function within the DOM element to locate all <input>
elements.
- Iterate through them, summing up their values (converted to integers).
For example, if your HTML looks like this:
<div id="coolStuff">
<asp:Repeater ... >
</div>
The corresponding JavaScript code would be something along these lines:
var container = document.getElementById("coolStuff");
var inputs = container.getElementsByTagName("input");
var sum = 0;
for (var i = 0; i < inputs.length; i++) {
sum += inputs[i].value;
}
alert(sum);
This script doesn't validate whether the <input>
elements are truly of type text
or if the entered values are numerical. These aspects are left as challenges for the reader ;)
Edit: If each "line" outputted by the Repeater
has multiple text boxes and you want to only sum the values within one group, some adjustments to the script are needed. Here are a couple of possible solutions - choose one:
If you know the exact number of <input>
elements in each "line," modify the client-side loop to iterate over every Nth element. For instance, to sum only the last of three fields in each line:
for (var i = 2; i < inputs.length; i += 3)
Add a class
attribute to the <input>
elements that should contribute to the total sum in your markup. Then, within the loop, check inputs[i].className
to determine if that field should be included.