looping through variable question

L

ll

Hi,
I have a radio button question. I have about 20 sets (20 questions,
each with a set of 4 radio buttons) and would like to loop through
them in doing a form validation. Here is the code I'm currently
using. The inner loop (which cycles through the 4 radio button set)
works fine, but I'm having a bit of a time with the outer loop (which
is designed to cycle through all of the 20 questions). I've commented
out the part that is giving the trouble (the establishing the
variable's value and the closing bracket). The name of the buttons
should go from "Out1_a, Out2_a, etc.

Thanks for any help you may be able to provide,
Louis


------------------------------

<script>
function checkform()
{
OutcomeOK=false;

//begin outer loop below
for (x=1;x<4;x++) {

//begin inner loop below
for (a=0;a<4;a++) {

//set up variable that changes according to row
//Out="Out"+x+"_a";
//Out=Out
//var Out=Out1_a

//alert(Out)
if (document.form.Out[a].checked) OutcomeOK=true;
}
if (!OutcomeOK) {
alert('Please make a selection for each Learning Outcome. Your
favorite Ice Cream flavor is a required field. Please try again.');
event.returnValue=false;
}

//}

return true;
}
</script>
 
S

SAM

ll a écrit :
Hi,
I have a radio button question. I have about 20 sets (20 questions,
each with a set of 4 radio buttons) and would like to loop through
them in doing a form validation.


<script type="text/javascript">

function checkform( f ) // f is the form to check
{
var txt1 = 'Please make a selection for the Learning Outcome :',
txt2 = '\nYour favorite Ice Cream flavor is a required field.'+
'\nPlease try again.'
var L = f.length+1;
for( var i=1; i<L; i++) // for each element of the form
{
if(f.type == 'radio' && // if it is a radio-button and
f.name.indexOf('Out')>=0) // its name contents 'Out'
{
var OK = false;
var r = f.elements['Out' + i + 'a']; // radios collection #i
for( var j=0; j<r.length; i++) // for each radio of them
if(r[j].checked) OK = true; // is there one checked ?
if(!OK) { // if not :
alert(txt1 + r.name +txt2); // alert
r[0].focus(); // go to it
return false; // stop to submit
}
}
}
return true; // if Oll is Korrect send 'true'
}
</script>



<form onsubmit="return checkform(this);" blah >
 
L

ll

ll a écrit :
Hi,
I have a radio button question. I have about 20 sets (20 questions,
each with a set of 4 radio buttons) and would like to loop through
them in doing a form validation.

<script type="text/javascript">

function checkform( f ) // f is the form to check
{
var txt1 = 'Please make a selection for the Learning Outcome :',
txt2 = '\nYour favorite Ice Cream flavor is a required field.'+
'\nPlease try again.'
var L = f.length+1;
for( var i=1; i<L; i++) // for each element of the form
{
if(f.type == 'radio' && // if it is a radio-button and
f.name.indexOf('Out')>=0) // its name contents 'Out'
{
var OK = false;
var r = f.elements['Out' + i + 'a']; // radios collection #i
for( var j=0; j<r.length; i++) // for each radio of them
if(r[j].checked) OK = true; // is there one checked?
if(!OK) { // if not :
alert(txt1 + r.name +txt2); // alert
r[0].focus(); // go to it
return false; // stop to submit
}
}
}
return true; // if Oll is Korrect send 'true'}

</script>

<form onsubmit="return checkform(this);" blah >


Thanks for your post in this. I had a question -
in this line:
"if(f.type == 'radio' && f.name.indexOf('Out')>=0)"
would "element" need to be inserted in the dot notation, such as:
"if(f.element.type == 'radio' &&
f.element.name.indexOf('Out')>=0)"?

Also, as "f" is the form, does "f" look for different elements in a
form or different forms?

Thanks for your help,
Louis
 
R

RobG

ll a écrit :
<script type="text/javascript">
function checkform( f )            // f is the form to check
{
var txt1 = 'Please make a selection for the Learning Outcome :',
     txt2 = '\nYour favorite Ice Cream flavor is a required field.'+
            '\nPlease try again.'
var L = f.length+1;
for( var i=1; i<L; i++)            // for each element ofthe form
    {
    if(f.type == 'radio' &&      // if it is a radio-button and
      f.name.indexOf('Out')>=0)  // its name contents 'Out'
        {
        var OK = false;
        var r = f.elements['Out' + i + 'a']; // radios collection #i
        for( var j=0; j<r.length; i++)          // for each radio of them
          if(r[j].checked) OK = true;           // is there one checked ?
        if(!OK) {                               // if not :
           alert(txt1 + r.name +txt2);       //    alert
           r[0].focus();                        //    go to it
           return false;                        //    stop to submit
           }
        }
     }
return true;  // if Oll is Korrect  send 'true'}
</script>

<form onsubmit="return checkform(this);" blah >


Thanks for your post in this.  I had a question -
in this line:
"if(f.type == 'radio' &&   f.name.indexOf('Out')>=0)"
would "element" need to be inserted in the dot notation, such as:
"if(f.element.type == 'radio' &&
f.element.name.indexOf('Out')>=0)"?


No. The members of the form's elements collection are made available
as indexed properties of the form object too, so f[0] and
f.elements[0] are the same thing. However the second is preferred.

Also, as "f" is the form, does "f" look for different elements in a
form or different forms?


It returns the ith element of the form. To get the ith form, use the
document.forms collection.

Here is another approach to a solution:

<script type="text/javascript">

function checkAnswers( f ) {

// Start by getting all the inputs
var inputs = f.getElementsByTagName('input');
var results = {};
var unchecked = [];
var el, elName;

// Loop over them to get the radio buttons
for (var i=0, len=inputs.length; i<len; i++) {
el = inputs;
elName = el.name;

// If it's a radio button
if (el.type == 'radio' && elName.length > 0) {

// If haven't added to results object yet, add it
if (!results.hasOwnProperty(elName)) {
results[elName] = undefined;
}

// If it's checked, add its value to results object
if (el.checked) {
results[elName] = el.value;
}
}
}

// Make sure all the named properties have a value
for (var p in results) {
if (!results[p]) {
unchecked.push(p);
}
}

// Report whether all questions have been answered
if (unchecked.length) {
alert('The following haven\'t been answered: \n' + unchecked);
} else {
alert('All questions answered');
}
}
</script>

<form id="f0" action=""><div>
<input type="radio" name="q0">0 - 0<br>
<input type="radio" name="q0">0 - 1<br>
<input type="radio" name="q0">0 - 2<br>
<input type="radio" name="q1">1 - 0<br>
<input type="radio" name="q1">1 - 1<br>
<input type="radio" name="q1">1 - 2<br>
<input type="button" value="collect..." onclick="
checkAnswers(this.form);
">
<input type="reset">
</div></form>
 
L

ll

ll a écrit :
Hi,
I have a radio button question. I have about 20 sets (20 questions,
each with a set of 4 radio buttons) and would like to loop through
them in doing a form validation.
<script type="text/javascript">
function checkform( f ) // f is the form to check
{
var txt1 = 'Please make a selection for the Learning Outcome :',
txt2 = '\nYour favorite Ice Cream flavor is a required field.'+
'\nPlease try again.'
var L = f.length+1;
for( var i=1; i<L; i++) // for each element of the form
{
if(f.type == 'radio' && // if it is a radio-button and
f.name.indexOf('Out')>=0) // its name contents 'Out'
{
var OK = false;
var r = f.elements['Out' + i + 'a']; // radios collection #i
for( var j=0; j<r.length; i++) // for each radio of them
if(r[j].checked) OK = true; // is there one checked ?
if(!OK) { // if not :
alert(txt1 + r.name +txt2); // alert
r[0].focus(); // go to it
return false; // stop to submit
}
}
}
return true; // if Oll is Korrect send 'true'}
</script>
<form onsubmit="return checkform(this);" blah >

Thanks for your post in this. I had a question -
in this line:
"if(f.type == 'radio' && f.name.indexOf('Out')>=0)"
would "element" need to be inserted in the dot notation, such as:
"if(f.element.type == 'radio' &&
f.element.name.indexOf('Out')>=0)"?


No. The members of the form's elements collection are made available
as indexed properties of the form object too, so f[0] and
f.elements[0] are the same thing. However the second is preferred.
Also, as "f" is the form, does "f" look for different elements in a
form or different forms?


It returns the ith element of the form. To get the ith form, use the
document.forms collection.

Here is another approach to a solution:

<script type="text/javascript">

function checkAnswers( f ) {

// Start by getting all the inputs
var inputs = f.getElementsByTagName('input');
var results = {};
var unchecked = [];
var el, elName;

// Loop over them to get the radio buttons
for (var i=0, len=inputs.length; i<len; i++) {
el = inputs;
elName = el.name;

// If it's a radio button
if (el.type == 'radio' && elName.length > 0) {

// If haven't added to results object yet, add it
if (!results.hasOwnProperty(elName)) {
results[elName] = undefined;
}

// If it's checked, add its value to results object
if (el.checked) {
results[elName] = el.value;
}
}
}

// Make sure all the named properties have a value
for (var p in results) {
if (!results[p]) {
unchecked.push(p);
}
}

// Report whether all questions have been answered
if (unchecked.length) {
alert('The following haven\'t been answered: \n' + unchecked);
} else {
alert('All questions answered');
}}

</script>

<form id="f0" action=""><div>
<input type="radio" name="q0">0 - 0<br>
<input type="radio" name="q0">0 - 1<br>
<input type="radio" name="q0">0 - 2<br>
<input type="radio" name="q1">1 - 0<br>
<input type="radio" name="q1">1 - 1<br>
<input type="radio" name="q1">1 - 2<br>
<input type="button" value="collect..." onclick="
checkAnswers(this.form);
">
<input type="reset">
</div></form>



Thanks for your help in this.
Are "results" and "unchecked" both arrays?

Does the first example have any errors or is it more likely the way in
which I may be adapting it to use with my form? I used the second
example, and it worked form me, although when I plugged the first code
example in place of the second (using the same form, but with form
name changed to "f") it gave me "length is null or not an object" in
this line: var r = f.element['q' + i]; // radios collection #i

Thanks
Louis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top