Help using looping in JS

Please support our JavaScript / DHTML / AJAX advertiser: $6.99 Domain Names at 1&1. Includes Free Privacy. Save Now!
Reply

Join Date: Mar 2007
Posts: 22
Reputation: enggars is an unknown quantity at this point 
Solved Threads: 0
enggars enggars is offline Offline
Newbie Poster

Help using looping in JS

 
0
  #1
May 8th, 2007
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...
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 3,266
Reputation: MidiMagic has a spectacular aura about MidiMagic has a spectacular aura about 
Solved Threads: 170
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Nearly a Senior Poster

Re: Help using looping in JS

 
0
  #2
May 10th, 2007
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 2:28 am. Reason: pick up the pieces
Daylight-saving time uses more gasoline
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 22
Reputation: enggars is an unknown quantity at this point 
Solved Threads: 0
enggars enggars is offline Offline
Newbie Poster

Re: Help using looping in JS

 
0
  #3
May 11th, 2007
Originally Posted by MidiMagic View Post
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....
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 20
Reputation: smalldog is an unknown quantity at this point 
Solved Threads: 1
smalldog smalldog is offline Offline
Newbie Poster

Re: Help using looping in JS

 
0
  #4
May 11th, 2007
Use this
for(i=1;i<3;i++){
document.getElementById('hidsum'+i).checked or .value ...
}
Instead of
document.form1.hidsum[i]
and don't forget define id (hidsum1,hidsum2..)
Last edited by smalldog; May 11th, 2007 at 8:06 pm.
Join a forum for webmasters and developers
HTML Forum
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 20
Reputation: smalldog is an unknown quantity at this point 
Solved Threads: 1
smalldog smalldog is offline Offline
Newbie Poster

Re: Help using looping in JS

 
0
  #5
May 11th, 2007
the reason is, you need probably count from 1 not from 0 like this:

for(i=1;i<3;i++){
Join a forum for webmasters and developers
HTML Forum
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 3,266
Reputation: MidiMagic has a spectacular aura about MidiMagic has a spectacular aura about 
Solved Threads: 170
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Nearly a Senior Poster

Re: Help using looping in JS

 
0
  #6
May 12th, 2007
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 12:30 pm.
Daylight-saving time uses more gasoline
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 22
Reputation: enggars is an unknown quantity at this point 
Solved Threads: 0
enggars enggars is offline Offline
Newbie Poster

Re: Help using looping in JS

 
0
  #7
May 13th, 2007
Originally Posted by MidiMagic View Post
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.....
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 3,266
Reputation: MidiMagic has a spectacular aura about MidiMagic has a spectacular aura about 
Solved Threads: 170
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Nearly a Senior Poster

Re: Help using looping in JS

 
0
  #8
May 14th, 2007
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?
Daylight-saving time uses more gasoline
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 22
Reputation: enggars is an unknown quantity at this point 
Solved Threads: 0
enggars enggars is offline Offline
Newbie Poster

Re: Help using looping in JS

 
0
  #9
May 16th, 2007
Originally Posted by MidiMagic View Post
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 3,266
Reputation: MidiMagic has a spectacular aura about MidiMagic has a spectacular aura about 
Solved Threads: 170
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Nearly a Senior Poster

Re: Help using looping in JS

 
0
  #10
May 16th, 2007
It won't be 0 if you take out the semicolon.
Daylight-saving time uses more gasoline
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2520 | Replies: 9
Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC