| | |
Help using looping in JS
Please support our JavaScript / DHTML / AJAX advertiser: $6.99 Domain Names at 1&1. Includes Free Privacy. Save Now!
![]() |
•
•
Join Date: Mar 2007
Posts: 22
Reputation:
Solved Threads: 0
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...
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...
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:
Notice that array indexes begin with 0, not 1.
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)
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; };
Last edited by MidiMagic; May 10th, 2007 at 2:28 am. Reason: pick up the pieces
Daylight-saving time uses more gasoline
•
•
Join Date: Mar 2007
Posts: 22
Reputation:
Solved Threads: 0
•
•
•
•
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:
Notice that array indexes begin with 0, not 1.JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
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; };
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....
•
•
Join Date: May 2007
Posts: 20
Reputation:
Solved Threads: 1
Use this
Instead of
and don't forget define id (hidsum1,hidsum2..)
•
•
•
•
for(i=1;i<3;i++){
document.getElementById('hidsum'+i).checked or .value ...
}
•
•
•
•
document.form1.hidsum[i]
Last edited by smalldog; May 11th, 2007 at 8:06 pm.
•
•
Join Date: May 2007
Posts: 20
Reputation:
Solved Threads: 1
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".
"'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
•
•
Join Date: Mar 2007
Posts: 22
Reputation:
Solved Threads: 0
•
•
•
•
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".
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.....
You have a bug. There must never be a semicolon before an else statement.
If the value of
If there is only one input box, why are you bothering to find the sum?
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
•
•
Join Date: Mar 2007
Posts: 22
Reputation:
Solved Threads: 0
•
•
•
•
You have a bug. There must never be a semicolon before an else statement.
If the value ofdocument.form1.sum.lengthis 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?
Well okay then, i think i'll just hide the total box if the document.form1.sum.length equal zero.
Thanks, you're magic
![]() |
Similar Threads
- Newbie - looping and array [very simple] problem (PHP)
- Looping through arrays (PHP)
- Help On Looping (C++)
- Looping and data merging (Python)
- Start-up is Looping, UGH! (Windows NT / 2000 / XP)
- Winsock and looping issues unsure how to resolve (Visual Basic 4 / 5 / 6)
- Need some help with putting a looping into a converter program. (C)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: JavaScript Help with display
- Next Thread: Running PHP
Views: 2520 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for JavaScript / DHTML / AJAX
ajax ajaxcode ajaxhelp animate array automatically blackjack box bug calendar column content cookies css design detect developer div dom download dynamic element error explorer explosion file firefox flash focus font form function game getelementsbytagname google gwt hangman hide hijack html iframe image images index insertbefore internet internet-explorer java javascript javascripts jquery js list listbox maps marquee match mimic modal mp3 multiple mysql offline onclick onerror onmouseover parameters pass photo php player position post print programming regex resize script scroll scrollbar search select selection show size sorting synchronous text tooltip tweet variables vb.net video web webservice website window xml xspf \n






