Previous Example-|-Next Example-|-Return to Chapter Listing

Example 16.2:
You Can't Name a Form Within a Function


We are using three different ways to refer to information in a form. Type something into the form, then click on the buttons to display an Alert box showing that information. All three methods may work on your browser. But on some browsers the first two will not work. We advise you to avoid the first two methods.

This one will give you an error:

This one will give you an error, too:

This one will work:


This is the script we used. In the HEAD... <SCRIPT LANGUAGE="JAVASCRIPT"> <!-- function What1(gertrude) { alert(document.fred.elements[0].value) } function What2(gertrude) { alert(document.forms[0].elements[0].value) } function What3(gertrude) { alert(gertrude.elements[0].value) } //--> </SCRIPT> Then this form: <FORM NAME="fred"> <input type="text" size="30"><P> <I>This one will give you an error:</I><BR> <input type="button" value="fred.elements[0].value" onclick="What1(this.form)"><P> <I>This one will give you an error, too:</I><BR> <input type="button" value="forms[0].elements[0].value" onclick="What2(this.form)"><P> <I>This one will work:</I><BR> <input type="button" value="gertrude.elements[0].value" onclick="What3(this.form)"> </FORM>
Previous Example-|-Next Example-|-Return to Chapter Listing