Hi i have a script like this :

function add()
{
c=document.form1.status1.checked;
d=document.form1.status2.checked;
if(c!=1)
document.form1.hidsum1.value=0;
else document.form1.hidsum1.value=document.form1.sum1.value
if(d!=1) document.form1.hidsum2.value=0;
else document.form1.hidsum2.value=document.form1.sum2.value
a=document.form1.hidsum1.value;
b=document.form1.hidsum2.value;
var e = parseInt(a)+parseInt(b);
document.form1.total.value=e;
}

And it works though. But i want to use looping to simplify the code.
Anybody can helps? i have no idea where to start.
Thanks for the help...

Recommended Answers

All 9 Replies

See my thread (third post):

An array of arrays of of radio buttons

It's essentially the same thing.

Give both checkboxes the same name, both hidsum boxes the same name, and both sum boxes the same name. Then use Javascript as follows:

function add()
  var i, d, e;
  e = 0;
  for(i=0;i<2;i++){
    if(document.form1.status[i].checked) document.form1.hidsum[i].value=0
    else document.form1.hidsum[i]1.value=document.form1.sum[i].value;
    a=document.form1.hidsum[i].value;
    e = e + parseInt(a);
  };
  document.form1.total.value=e;
};

Notice that array indexes begin with 0, not 1.

See my thread (third post):

An array of arrays of of radio buttons

It's essentially the same thing.

Give both checkboxes the same name, both hidsum boxes the same name, and both sum boxes the same name. Then use Javascript as follows:

function add()
  var i, d, e;
  e = 0;
  for(i=0;i<2;i++){
    if(document.form1.status[i].checked) document.form1.hidsum[i].value=0
    else document.form1.hidsum[i]1.value=document.form1.sum[i].value;
    a=document.form1.hidsum[i].value;
    e = e + parseInt(a);
  };
  document.form1.total.value=e;
};

Notice that array indexes begin with 0, not 1.

Hello....
I've modify the code as your instruction,
but it gives me some error code :

'document.form1.status' is null or not an object

The problem occurs after i change the code, so i believe there are some mistakes on the javascript instead the form.
Need your help, thanks....

Use this

for(i=1;i<3;i++){
document.getElementById('hidsum'+i).checked or .value ...
}

Instead of

document.form1.hidsum

and don't forget define id (hidsum1,hidsum2..)

the reason is, you need probably count from 1 not from 0 like this:

for(i=1;i<3;i++){

JavaScript always counts from 0 on arrays and object lists.

"'document.form1.status' is null or not an object"

This indicates that you forgot to change the names in the html to match the change in the JavaScript. You were supposed to rename your html objects so they have the same name "status" instead of having the names "status1" and "status2".

JavaScript always counts from 0 on arrays and object lists.

"'document.form1.status' is null or not an object"

This indicates that you forgot to change the names in the html to match the change in the JavaScript. You were supposed to rename your html objects so they have the same name "status" instead of having the names "status1" and "status2".

Hello...
Okay the code is working, except on single inputbox situation.
I use the document.form1.sum.length to set the looping limitation.
The problem is when there is only one inputbox, the total is always equal to 0.

function add(){
var i, d, e;
e = 0;
for(i=0;i<document.form1.sum.length;i++){
if(document.form1.status.checked)
document.form1.hidsum.value=document.form1.sum.value;
else document.form1.hidsum.value=0;
a=document.form1.hidsum.value;
e = e + parseInt(a);
};
document.form1.total.value=e;
};

Please help me with this, thanks.....

You have a bug. There must never be a semicolon before an else statement.

If the value of document.form1.sum.length is zero, the loop will never execute. The termination condition is met when the loop is entered.

If there is only one input box, why are you bothering to find the sum?

You have a bug. There must never be a semicolon before an else statement.

If the value of document.form1.sum.length is zero, the loop will never execute. The termination condition is met when the loop is entered.

If there is only one input box, why are you bothering to find the sum?

Hi...
Well okay then, i think i'll just hide the total box if the document.form1.sum.length equal zero.
Thanks, you're magic :)

It won't be 0 if you take out the semicolon.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.