Tutorial 1: Feedback Using Pull-Down Menus
The following tutorial presupposes that you have
some familiarity with HTML authoring.
This an example of an exercise that provides immediate feedback to the user's answer in an
input text field ("blank") next to the pull-down menu storing the options.
This exercise presents a dialogue, but it can be adapted for other grammatical or
communicational goals.
Click here to see the finished product: template1.html
Some points to remember:
<SCRIPT LANGUAGE = "JavaScript">
<!--
function choiceA(form) {
form.first_value.value = form.one.options[form.one.selectedIndex].value
}What this function does is to write into a text field named "first_value" (the blank next to the pull-down menu) the value (or feedback that you wrote in the HTML for each of the possible options) of any selected option from a form (in this case, a pull-down menu) named "one." Again, all these names are arbitrary. So, when the user selects an item for the first pull-down menu, its feedback automatically appears in the blank next to it.
<SELECT VALUE = "one" onChange="choiceA(this.form)">
<OPTION VALUE = "Choose an option.">--Choose an option--
<OPTION VALUE = "Feedback to First Option.">--First Option.
<OPTION VALUE = "Feedback to Second Option.">--Second option.
<OPTION VALUE = "Feedback to Third Option.">--Third option.
</SELECT>
As you can see here, "one" is the name of this form (pull-down menu). The function choiceA is present in the onChange event: what triggers the function when the user selects (changes) an option. For each one of the options you have a value attribute: this is where you write the feedback for each option. These different values are the feedback messages the user will see in the text filed.
<INPUT NAME = "first_value" SIZE = "35" VALUE = "Feedback.">
The name of this form element is "first_value." This is the name that we saw at the beginning of the scripting. The value attribute, that is, what appears in the "blank" when the web page is loaded, is the word "Feedback" (remember that you can change these names to something more significant to you). This content will change once the user selects an option form the pull-down menu.
function getExplanation(msg) {
document.forms[0].first_value.value = msg
document.forms[0].first_value.focus()
document.forms[0].first_value.select()
}The function is called getExplanation, but you can call it any other way. Two important things to consider here:
- first_value corresponds to the first text field. That is, the message (translation, explanation, etc.) of this function will be shown in that field.
- focus and select selects (highlights) the content of the blank 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 form in the HTML document. The index number of forms in a HTML document starts with "0". So, let's say that you want to include a link in a hypothetical fourth question: its form number would be 3, not 4. Following the naming order of the forms, you would have something like this:
function getExplanation3(msg) {
document.forms[3].fourth_value.value = msg
document.forms[3].fourth_value.focus()
document.forms[3].fourth_value.select()
}
You will have to change the name of the function itself (in this case, it could be getExplanation3 if this is the third link to which you are assigning this type of function), since functions cannot have identical names in JavaScript.
<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>