I'm creating a form with dynamic radio buttons, which is working well when posting the form, and retrieving the results with PHP, however, I would like to validate the form to ensure at least one option from each radio button group has been selected, but I can't seem to work out how to iterate through the objects to check.
An example of the HTML looks like this...
... etc...
Whilst I understand how to iterate through an object in Javascript if it wasn't an array, i.e. I'd just used 'Answer' rather than Answer[1] and Answer[2], I thought I could just treat it as a multi-dimensional array, but that isn't working... e.g. to check if the first radio button in question 1 had been checked, I could use:
... but that just gives me an 'null or not an object' error
Any ideas gratefully received...
An example of the HTML looks like this...
Code:
<tr><td align="left"><strong>Question 1</strong></td></tr><tr><td align="left" width="60%" valign="top">What colour is grass?</td><td align="left" valign="top">
<input type="radio" name="Answer[1]" id="Answer[1]" value="B" />Blue<br/>
<input type="radio" name="Answer[1]" id="Answer[1]" value="L" />Black<br/>
<input type="radio" name="Answer[1]" id="Answer[1]" value="G" />Green<br/>
<input type="radio" name="Answer[1]" id="Answer[1]" value="P" />Purple<br/>
</td></tr><tr><td> </td></tr>
</tr><tr><td colspan="2" style="background-color: #ffffff;"> </td></tr><tr><td align="left"><strong>Question 2</strong></td></tr>
<tr><td align="left" width="60%" valign="top">Is the sky blue?</td><td align="left" valign="top">
<input type="radio" name="Answer[2]" id="Answer[2]" value="Y" />Yes<br/>
<input type="radio" name="Answer[2]" id="Answer[2]" value="N" />No<br/>
</td></tr>
... etc...
Whilst I understand how to iterate through an object in Javascript if it wasn't an array, i.e. I'd just used 'Answer' rather than Answer[1] and Answer[2], I thought I could just treat it as a multi-dimensional array, but that isn't working... e.g. to check if the first radio button in question 1 had been checked, I could use:
Code:
var ans = document.inputform.Answer[1][0].checked;
... but that just gives me an 'null or not an object' error
Any ideas gratefully received...