944,182 Members | Top Members by Rank

Ad:
May 8th, 2007
0

Help using looping in JS

Expand Post »
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...
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
enggars is offline Offline
22 posts
since Mar 2007
May 10th, 2007
0

Re: Help using looping in JS

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:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function add()
  2. var i, d, e;
  3. e = 0;
  4. for(i=0;i<2;i++){
  5. if(document.form1.status[i].checked) document.form1.hidsum[i].value=0
  6. else document.form1.hidsum[i]1.value=document.form1.sum[i].value;
  7. a=document.form1.hidsum[i].value;
  8. e = e + parseInt(a);
  9. };
  10. document.form1.total.value=e;
  11. };
Notice that array indexes begin with 0, not 1.
Last edited by MidiMagic; May 10th, 2007 at 3:28 am. Reason: pick up the pieces
Reputation Points: 730
Solved Threads: 181
Nearly a Senior Poster
MidiMagic is offline Offline
3,314 posts
since Jan 2007
May 11th, 2007
0

Re: Help using looping in JS

Click to Expand / Collapse  Quote originally posted by MidiMagic ...
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:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function add()
  2. var i, d, e;
  3. e = 0;
  4. for(i=0;i<2;i++){
  5. if(document.form1.status[i].checked) document.form1.hidsum[i].value=0
  6. else document.form1.hidsum[i]1.value=document.form1.sum[i].value;
  7. a=document.form1.hidsum[i].value;
  8. e = e + parseInt(a);
  9. };
  10. document.form1.total.value=e;
  11. };
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....
Reputation Points: 10
Solved Threads: 0
Newbie Poster
enggars is offline Offline
22 posts
since Mar 2007
May 11th, 2007
0

Re: Help using looping in JS

Use this
Quote ...
for(i=1;i<3;i++){
document.getElementById('hidsum'+i).checked or .value ...
}
Instead of
Quote ...
document.form1.hidsum[i]
and don't forget define id (hidsum1,hidsum2..)
Last edited by smalldog; May 11th, 2007 at 9:06 pm.
Reputation Points: 9
Solved Threads: 1
Newbie Poster
smalldog is offline Offline
20 posts
since May 2007
May 11th, 2007
0

Re: Help using looping in JS

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

Quote ...
for(i=1;i<3;i++){
Reputation Points: 9
Solved Threads: 1
Newbie Poster
smalldog is offline Offline
20 posts
since May 2007
May 12th, 2007
0

Re: Help using looping in JS

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".
Last edited by MidiMagic; May 12th, 2007 at 1:30 pm.
Reputation Points: 730
Solved Threads: 181
Nearly a Senior Poster
MidiMagic is offline Offline
3,314 posts
since Jan 2007
May 13th, 2007
0

Re: Help using looping in JS

Click to Expand / Collapse  Quote originally posted by MidiMagic ...
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[i].checked)
document.form1.hidsum[i].value=document.form1.sum[i].value;
else document.form1.hidsum[i].value=0;
a=document.form1.hidsum[i].value;
e = e + parseInt(a);
};
document.form1.total.value=e;
};

Please help me with this, thanks.....
Reputation Points: 10
Solved Threads: 0
Newbie Poster
enggars is offline Offline
22 posts
since Mar 2007
May 14th, 2007
0

Re: Help using looping in JS

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?
Reputation Points: 730
Solved Threads: 181
Nearly a Senior Poster
MidiMagic is offline Offline
3,314 posts
since Jan 2007
May 16th, 2007
0

Re: Help using looping in JS

Click to Expand / Collapse  Quote originally posted by MidiMagic ...
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
enggars is offline Offline
22 posts
since Mar 2007
May 16th, 2007
0

Re: Help using looping in JS

It won't be 0 if you take out the semicolon.
Reputation Points: 730
Solved Threads: 181
Nearly a Senior Poster
MidiMagic is offline Offline
3,314 posts
since Jan 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: JavaScript Help with display
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Running PHP





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC