background
|
|
Hosting
My-Hosts.com
Cheap Hosting, domain registration and design. Check this out for more details.
More icon

Computer
|
|
Up Image
Navigation
Search this Site
Enter your search terms

Site Breadcrumb - You are here
|
Thau's JavaScript Tutorial
Lesson 5

by Thau!

Page 2 — Introduction to Forms

Forms are part of the HTML 1.0 specification. Many people don't know about them, however, because there's a misconception that they're only useful if you (or someone you know) can do server-side CGI programming. As June points out in her forms tutorial, forms can be fun even if you aren't a CGI programmer. More important for this tutorial, JavaScript can be used to add all sorts of functionality to forms without the help of server-side CGI.

If you don't know how forms work, check out Jay's tutorial and try writing some forms yourself. I'm going to assume that you know the basics.

The first thing you need to know about forms and JavaScript is that forms, like images, are stored in an array of objects. Just like you can refer to the first image on a page by calling it window.document.images[0], you can refer to the first form on a page using window.document.forms[0]. Similarly, just like you can name images, you can name forms. For an example if this form ...

... is written like this,


<form name="first_form">
<input type="text" name="first_text" size="40" 
value="Power to the primates!">
</form>
You can refer to the form in either of these two ways:

var the_form = window.document.forms[0];
var the_same_form = window.document.first_form;

Although being able to refer to forms is sometimes useful, more often you want to refer to elements inside of forms, such as the text field in the last example. Here's an example of manipulating the value of a text field.

next page»