Tutorial 2: Feedback Using Radio Buttons
The following tutorial presupposes that you have
some familiarity with HTML authoring.
This is an example of an exercise that provides immediate feedback to the user's answer in
a textarea box next to the radio buttons showing the options.
This template can be adapted for different grammatical or communicational goals.
Click here to see the finished product: template2.html
Some points to remember:
<SCRIPT LANGUAGE = "JavaScript">
<!--
function choiceA(form) {
for (var i = 0;i < form.one.length; i++) {
if (form.one[i].checked) {
break
}
}
document.forms[1].first.value = document.forms[0].one[i].value
}What this function does is to to write into a textarea box named "first" the value (or feedback that you wrote in the HTML for each of the three different radio buttons) of the option selected (the radio button clicked, named "one"). When the user clicks on any of the three radio buttons of question 1, the feedback corresponding to that selection immediately appears in the textarea box.
<INPUT TYPE = "radio" NAME = "one" VALUE = "Feedback for First Option." onClick = "choiceA(this.form)">First option<BR>
<INPUT TYPE = "radio" NAME = "one" VALUE = "Feedback for Second Option." onClick = "choiceA(this.form)">Second Option.<BR>
<INPUT TYPE = "radio" NAME = "one" VALUE = "Feedback for Third Option." onClick = "choiceA(this.form)">Third Option.As you can see here, the three radio buttons are named "one". The function choiceA is present in the onClick event: when the user makes a selection by clicking on a radio button, its function is triggered. For each one of the radio buttons there is a value attribute: this is where you write the feedback for each radio button. These different values are the the feedback that the user will see in the textarea box.
<TEXTAREA = "first" ROWS = "5" COLS = "25" WRAP = "on">Feedback</TEXTAREA>
The name of this textarea is "first." This is the name that we saw at the beginning of the scripting. Between the textarea tags <TEXTAREA></TEXTAREA> we place the word "Feedback," which appears inside the textarea box when the web page is loaded. This text will be substituted by the value of the radio buttons: in our script, "Feedback to First Option," "Feedback to Second Option," etc.
function getExplanation(msg) {
document.forms[0].second.value = msg
document.forms[0].second.focus()
document.forms[0].second.select()
}The function is called getExplanation, but you can call it any other way. Two important things to consider here:
- second corresponds to the second textarea box. That is, the message (translation, explanation, etc.) of this function will be shown in the textarea box next to the second set of radio buttons.
- focus and select select (highlight) the content of textarea box with its new feedback. This is useful to direct the attention of the user to the explanation.
- document.forms[0] indicates the order of the forms in the HTML document. The index number of forms in a HTML document starts with "0". In our script we only have one pair of <FORM></FORM> tags, at the beginning and at the end of the <TABLE></TABLE> tags. If we had more <FORM></FORM> tags, each one should be referred to with its number: [0], [1], [2]. etc.
<A HREF = "#" onClick = "getExplanation('Explanation')">unknown_word</a>
- By adding the # sign to the <A HREF> tag, we are directing the link to the word itself.
- Then we add the JavaScript event onClick followed by the = sign and the function getExplanation. In the parentheses we write our explanation, translation, etc. for this particular word ("unknown_word"). When the user clicks on this link, the function getExplanation gets implemented, writing into the text field "first_value" the message "Explanation."
// -->
</SCRIPT>
</HEAD>