/    Sign up×
Community /Pin to ProfileBookmark

Nested loops ? anyone

Hi all
This is my first post to the list.
I am an absolute beginner with Javascript and would appreciate some help with this loop/s
I now nothing about javascript syntax can’t get it to work.
I have a form with 7 questions, each question have to be checked Yes or No (input type =radio). If the user checks Yes, I have to know if s/he checks any other control for that question (3 checkboxes + 1 textbox)
for Each question the controlnames is QAnswer1_1 (radio Yes/No), QAnswer1_2 to QAnswer1_4 (checkboxes) and QAnswer1_5 (text)
So controls for Question no 7 is named QAnswer7_1 (radio Yes/No), and so on.

So if someone can show me how to combine the counters with “pre”/”remaining” text for the controls like in the Pseudocode, you’ll save the rest of the week for me (at least).

I just can’t get to grip with this.
‘Pseudocode **********************
QAnswer[n]’_[i]
if QAnswer[n]’
_
1 = “Yes then…
QAnswer[n]’_'[j] ‘Are there any Checkboxes checked?
*
**
********************************

[code=php]
for (var n=0;n<7;n++) { //this runs 7 times
for(var i=0;i<_frm.QAnswer1_1.length;i++) //
if(_frm.QAnswer1_1[i].checked)
_fd += “Ja/Nej: ” + _frm.QAnswer1_1[i].value + _n;
for(var i=0;i<_frm.QAnswer1_2.length;i++)
if(_frm.QAnswer1_2[i].checked)
_fd += “How Many: ” + _frm.num[i].value + _n;
}
[/code]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@ConRoyauthorJun 29.2004 — No takers this far, so let's redefine my Question.

How do I replace the numbers in "_frm.QAnswer1_1.value" with

countervalues like

_
frm.QAnswer+i+_+j+.value
Copy linkTweet thisAlerts:
@KorJun 29.2004 — Try this example (it is dinamically coded so you may insert as many groups as you want, 7 in your case - I use 3 here to shorten the replay:

[code=php]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script>
r=0;
function verif(f){//sets the number of groups according to the nr of radio elements
for(var i=0;i<f.elements.length;i++){
if(f.elements[i].type=='radio'){
r++
}
}
nr=r/2//nr of groups
for(var j=1;j<nr+1;j++){//starts from 1, as your elements' names start indenting from 1!!
if(f.elements['QAnswer'+j+'_1'][0].checked){
if((!f.elements['QAnswer'+j+'_2'].checked)&&(!f.elements['QAnswer'+j+'_3'].checked)&&(!f.elements['QAnswer'+j+'_4'].checked)){
alert('You have not checked at least one checkbox on Question nr.'+j);
return false;
}
if(f.elements['QAnswer'+j+'_5'].value==''){
alert('You have not complete the textfield on Question nr.'+j);
f.elements['QAnswer'+j+'_5'].focus();
return false;
}
}
}
}
</script>
</head>

<body>
<form onsubmit="return verif(this)">
<!-- group 1 -->
<input name="QAnswer1_1" type="radio" value="Yes">Yes<br>
<input name="QAnswer1_1" type="radio" value="No">No
<input name="QAnswer1_2" type="checkbox" value="">
<input name="QAnswer1_3" type="checkbox" value="">
<input name="QAnswer1_4" type="checkbox" value="">
<input name="QAnswer1_5" type="text"><br>
--------------------------------------------<br>
<!-- group 2 -->
<input name="QAnswer2_1" type="radio" value="Yes">Yes<br>
<input name="QAnswer2_1" type="radio" value="No">No
<input name="QAnswer2_2" type="checkbox" value="">
<input name="QAnswer2_3" type="checkbox" value="">
<input name="QAnswer2_4" type="checkbox" value="">
<input name="QAnswer2_5" type="text"><br>
--------------------------------------------<br>
<!-- group 2 -->
<input name="QAnswer3_1" type="radio" value="Yes">Yes<br>
<input name="QAnswer3_1" type="radio" value="No">No
<input name="QAnswer3_2" type="checkbox" value="">
<input name="QAnswer3_3" type="checkbox" value="">
<input name="QAnswer3_4" type="checkbox" value="">
<input name="QAnswer3_5" type="text"><br>
--------------------------------------------<br>
<br>
<input name="submit" type="submit">
</form>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@ConRoyauthorJun 29.2004 — YIPPEEEE !!!! ?

Fantastic! All beers to you ?

With a minimum of tweaking I've got it working. (lucky for me since I only do V?

Don't know if I dare to ask a another question, (two really).

I've inserted your's and another function in another jscript like this.
[code=php]
verif(f)
// how do I prevent the form to upload data here ?
previewForm(f)
[/code]

It loops through all controls just fine, but for the last confirmbox I always get "undefined" for verif(f).

For "confirm(previewForm(f));" I always get a box saying "true".

Is there a simple way so I dont hve too see these two boxes ?



[i]Originally posted by Kor [/i]

[B]Try this example (it is dinamically coded so you may insert as many groups as you want, 7 in your case - I use 3 here to shorten the replay:



[code=php]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script>
r=0;
function verif(f){//sets the number of groups according to the nr of radio elements
for(var i=0;i<f.elements.length;i++){
if(f.elements[i].type=='radio'){
r++
}
}
nr=r/2//nr of groups
for(var j=1;j<nr+1;j++){//starts from 1, as your elements' names start indenting from 1!!
if(f.elements['QAnswer'+j+'_1'][0].checked){
if((!f.elements['QAnswer'+j+'_2'].checked)&&(!f.elements['QAnswer'+j+'_3'].checked)&&(!f.elements['QAnswer'+j+'_4'].checked)){
alert('You have not checked at least one checkbox on Question nr.'+j);
return false;
}
if(f.elements['QAnswer'+j+'_5'].value==''){
alert('You have not complete the textfield on Question nr.'+j);
f.elements['QAnswer'+j+'_5'].focus();
return false;
}
}
}
}
</script>
</head>

<body>
<form onsubmit="return verif(this)">
<!-- group 1 -->
<input name="QAnswer1_1" type="radio" value="Yes">Yes<br>
<input name="QAnswer1_1" type="radio" value="No">No
<input name="QAnswer1_2" type="checkbox" value="">
<input name="QAnswer1_3" type="checkbox" value="">
<input name="QAnswer1_4" type="checkbox" value="">
<input name="QAnswer1_5" type="text"><br>
--------------------------------------------<br>
<!-- group 2 -->
<input name="QAnswer2_1" type="radio" value="Yes">Yes<br>
<input name="QAnswer2_1" type="radio" value="No">No
<input name="QAnswer2_2" type="checkbox" value="">
<input name="QAnswer2_3" type="checkbox" value="">
<input name="QAnswer2_4" type="checkbox" value="">
<input name="QAnswer2_5" type="text"><br>
--------------------------------------------<br>
<!-- group 2 -->
<input name="QAnswer3_1" type="radio" value="Yes">Yes<br>
<input name="QAnswer3_1" type="radio" value="No">No
<input name="QAnswer3_2" type="checkbox" value="">
<input name="QAnswer3_3" type="checkbox" value="">
<input name="QAnswer3_4" type="checkbox" value="">
<input name="QAnswer3_5" type="text"><br>
--------------------------------------------<br>
<br>
<input name="submit" type="submit">
</form>
</body>
</html>
[/code]
[/B][/QUOTE]
Copy linkTweet thisAlerts:
@KorJun 30.2004 — I don't quite understand... You want to prevent the upload if conditions are not fulfiled? The [b]return[/b] condition does that. If condition is not fulfiled, [b]return false[/b] will stop the code and will stop the HTML action, [i]submit[/i] in your case...
×

Success!

Help @ConRoy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 11.14,
social: @webDeveloperHQ,
});

legal: ({
terms: of use,
privacy: policy
analytics: Fullres
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: Anonymous,
tipped: article
amount: 1000 SATS,

tipper: @aldoushuxley,
tipped: article
amount: 1000 SATS,

tipper: Anonymous,
tipped: article
amount: 1000 SATS,
)...