Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checkin for radio buttons

Status
Not open for further replies.

FALCONSEYE

Programmer
Joined
Jul 30, 2004
Messages
1,158
Location
US
i am sorry, i guess it's friday and i am tired. here is my problem. i have a quiz that every answer has the following format


some question 1:
<input name="1" type="radio" value="a">1
<input name="1" type="radio" value="b">2
<input name="1" type="radio" value="c">3
<input name="1" type="radio" value="d">4

some question 2:
<input name="2" type="radio" value="a">1
<input name="2" type="radio" value="b">2
<input name="2" type="radio" value="c">3
<input name="2" type="radio" value="d">4

....

at the end, when the user clicks on submit, i want to check that every question is answered. there are 20 questions, and this is what i have so far

function checkAnswers() {
for (var i=1; i<21; i++)
{
if (document.quiz.toString(i).checked == false)
{
alert ("You must answer all of the questions!");
return false;
}

}
return true;
}

the above script doesn't return false even if i don't check any radio buttons. does anyone have an idea what goes on ?
thanks

 
Try this:
Code:
function checkAnswers()
{
for (var i=1; i<21; i++)
  {
  var onecheck = document.forms['quiz'].elements['' + i];
  var checked = false;
  for (var oi=0;oi<onecheck.length;oi++)
    {
    if (onecheck[oi].checked == true)
      {
      checked = true;
      break;
      }
    }
  if (checked == false)
    {
    alert ("You must answer all of the questions!");
    return false;
    }
  }
return true;    
}

I'd recommend naming your form elements something that DOESN'T start with a number to avoid problems, though.

Lee
 
function checkAnswers() {
var flgChecked = 0;
for (var i=1; i<21; i++){
strElement=toString(i);
for (var j=1;j<4;j++){
if(document.quiz.strElement[j].checked==true){
flgChecked = 1;
}
}
}
if(flgChecked==1){
alert("You must answer all the questions.");
}

.... I think I got that right.... with radio buttons, you have to check each ELEMENT of the radio button... in other words, given:
<input name="1" type="radio" value="a">1
<input name="1" type="radio" value="b">2
<input name="1" type="radio" value="c">3
<input name="1" type="radio" value="d">4

The first one would be document.form.name[0].checked
the second would be document.form.name[1].checked and so forth.



Just my 2¢

"In order to start solving a problem, one must first identify its owner." --Me
--Greg
 
There are several things that would need adjusted in your code, gbaughma. You don't want to check to see if the flag variable is 1, you want to check to see if it's zero. As well, the flag needs to be checked after each set of radio buttons to make sure that at least one is selected in the set. If any set has none selected, then you'd want the error message and return false;.

Lee
 
trollacious:
That's what I get for writing code off the top of my head. Sorry... I'm brain-dead today.



Just my 2¢

"In order to start solving a problem, one must first identify its owner." --Me
--Greg
 
Change the name of the radio buttons to start with a letter, not a number, and see what happens. What you're running into is one of the problems I mentioned in my first post.

Lee
 
trollacious:
You know, in looking back over my code, you're right. It sucked. <LOL>



Just my 2¢

"In order to start solving a problem, one must first identify its owner." --Me
--Greg
 
Nah, you had the main idea down, just needed a few tweaks to get what he wanted. :)#

Lee
 
Why won't you change the cause of the problem? In the time it's taken to discuss it here, you could have made the change on this page and the page that processes the choices.

Lee
 
I have found in the past that javascript really DOESN'T like numbers to be the first characters in variable names...

I had some code that just plain refused to work.... I was building a dynamic form to do vehicle scheduling, and had drop-downs that were named starting with the record number they were pulled from... i.e.

<select name="<%=recNo & "Drop"%>"

... when I later referenced that in my javascript with document.formname.123Drop.value it *really* didn't like it. Didn't work at all. I ended up having to name it "Drop123" or something like that.





Just my 2¢

"In order to start solving a problem, one must first identify its owner." --Me
--Greg
 
Yes, the element name starting with a digit is the most likely cause of the problem. Adding an underscore or letter to the beginning of the name should take care of the problem completely (it did when I tested the code I provided in my first response modified this way).

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top