On Jul 2, 6:18*am, ll <barn104_1...@y ahoo.comwrote:
On Jun 27, 5:12 pm, SAM <stephanemoriau x.NoAd...@wanad oo.fr.invalid>
wrote:
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 ofthe form
* * {
* * if(f[i].type == 'radio' && * * *// if it is a radio-button and
* * * f[i].name.indexOf(' Out')>=0) *// its name contents 'Out'
* * * * {
* * * * var OK = false;
* * * * var r = f[i].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[i].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="retur n checkform(this) ;" blah >
--
sm
Thanks for your post in this. *I had a question -
in this line:
"if(f[i].type == 'radio' && * f[i].name.indexOf(' Out')>=0)"
would "element" need to be inserted in the dot notation, such as:
"if(f[i].element[i].type == 'radio' &&
f[i].element[i].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[i]" 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.getElementsBy TagName('input' );
var results = {};
var unchecked = [];
var el, elName;
// Loop over them to get the radio buttons
for (var i=0, len=inputs.leng th; i<len; i++) {
el = inputs[i];
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.hasOw nProperty(elNam e)) {
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.leng th) {
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(th is.form);
">
<input type="reset">
</div></form>
--
Rob