View Full Version : Need a JavaScript for adding values...

09-01-2004, 06:14 PM
I am a newbie at JavaScript. I need to know where I can find a script (or a tutorial on how to build a script) that will sum up values for checked items only.

For instance... a contractor visits a form page that lists options that can be added to a project on which they wish to bid. I want the page to provide a total for the selected objects only.

In the future, I may want to link this script to a database so that the visitor can store their session. However, at this time, I simply want the page to provide a total. Can someone please help me with this?

Thanks ever so much!


09-01-2004, 08:10 PM
So your checkboxes look like this:

<input type="checkbox" name="project1">p1
<input type="checkbox" name="project2">p2
<input type="checkbox" name="project3">p3
<input type="checkbox" name="project4">p4

How do you associate a value with each checkbox? I'm assuming you want to sum some numbers up.. or do you just want to count them?

How to count:

function countCheckboxes() {
var count = 0;

var inputs = document.getElementsByTagName('input');
for(var i = 0; i < inputs.length; i++) {
if(inputs.getAttribute('type') == 'checkbox') {
if(inputs.checked) {

return count;

Hope that helps,

09-01-2004, 09:39 PM
Thank you for the input, Sadiq. Actually, I will assign monetary values to the select boxes (I assume that's how any JavaScript will acquire the values it should sum). The script should add the values in order to give a total for what the project would cost.

Since I will not need to retain any of the information as of yet and I do not need any of the form field info sent to me, I see no need to label the select boxes to reflect the actual item... just the cost of said item.