Tutorial 3: Feedback Using Alert Boxes
The following tutorial presupposes that you have
some familiarity with HTML authoring.
This an example of an exercise that provides immediate feedback by means of alert boxes to
the user's answer in an input text field ("blank").
This exercise presents a dialogue, but it can be adapted for other grammatical or
communicational goals.
Click here to see the finished product: template3.html
Some points to remember:
JavaScript is case sensitive, so you will have to be consistent using lower case or capital letter when naming your functions or calling values from forms. That applies also to accented words.
<SCRIPT LANGUAGE = "JavaScript">
<!--
function checkIt() {
var answer = document.forms[0].one.value
if (answer == "White" || answer == "White." || answer == "white" || answer == "white.") {
document.forms[0].one.value = "It wasn't that difficult, was it?"
document.forms[0].two.focus()
document.forms[0].two.select()
}-Inside the condition if we have foreseen four different ways the user can type the right answer, white: with or without a capital W, and with and without ending the answer with a period (.). The double equal sign == means "or".
-After that we instruct the script to write in the text field the feedback corresponding to this right answer:
document.forms[0].one.value = "It wasn't that difficult, was it?"-The last two lines makes the cursor jump (focus) to the second text field, named two, and highlight it (select).
else {
alert("Think, think...")
document.forms[0].one.focus()
document.forms[0].one.select()
}-The alert("your_message") event opens up the JavaScript alert window with its message inside: in this case, "Think, think...". After that, the cursor returns to the field and highlights it, so the user can try again.
See how the event onClick is incorporated inside the tags of the button to call the first function of the script:
<INPUT TYPE = "button" NAME = "click1" VALUE = "1." onClick = "checkIt()">
For the image we have the following tag:
<A HREF = "Javascript:checkIt2()"><IMG SRC = "../Colby/images/checkmrk1.gif" ALIGN = "top" BORDER = "0" HSPACE = "3" WIDTH = "18" HEIGHT = "17"></A>
In this case we make the image clickable by placing it within the <A HREF></A> tags. The "link" for the image is now the JavaScript coding "javascript"checkIt2()", that is, the calling of the second function of the script. So, when the user clicks on the image, the second function gets implemented.
// -->
</SCRIPT>
</HEAD>