Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q1574 How can I use an array to read in the input for multiple choice radio button questions, and then create a document to display those values by pushing a button?

You are here: irt.org | FAQ | JavaScript | Form | Q1574 [ previous next ]

Try something like:

<script language="JavaScript"><!--
var answers = new Array(10);
var correct = new Array('yes','no','yes','perhaps'....);

function compareAndShow() {
    var text = 'You did the following:<br>'
    for (i=0;i<10;i++) {
        text += '<br>Answer ' + (i+1) + 'was ';
        if (answers[i] == correct[i])
            text += 'correct';
        else
            text += 'incorrect';
   }
   var winid = window.open('','newwin');
   winid.document.open();
   winid.document.write(text);
   winid.document.close();
}
//--></script>

<form>
<input type="radio" name="q0" onClick="answers[0] = 'yes'">Yes
<input type="radio" name="q0" onClick="answers[0] = 'no'">No
<br>
<input type="radio" name="q2" onClick="answers[1] = 'yes'">Yes
<input type="radio" name="q1" onClick="answers[1] = 'no'">No
<input type="radio" name="q1" onClick="answers[1] = 'maybe'">Maybe
<input type="radio" name="q1" onClick="answers[1] = 'none'">None of your business
<br>
<input type="radio" name="q2" onClick="answers[2] = 'yes'">Yes
<input type="radio" name="q2" onClick="answers[2] = 'no'">No
.
.
.
<input type="button" onClick="compareAndShow()" VALUE="View Results">
</form>

©2018 Martin Webb