spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / javascript / beginning / chap6 / 112
[previous]
Developer News
MicrosoftÂ’s Automated Agent: Can We Talk?
Borland Finally Sells CodeGear
Red Hat Heads For The JON 2.0

HTML Forms: Interacting with the User

How It Works

Within the body of the page we define three forms. Each form is given a name, and contains a paragraph of text.

Within the definition of the tag, the window_onload() function is connected to the window object's onload event handler.

<BODY LANGUAGE=JavaScript onload="return window_onload()">

This means that when the page is loaded, our window_onload() function will be called. The window_onload() function is defined in a script block in the head of the page. Within this function we loop through the forms[] array. Just like any other JavaScript array, the forms[] array has a length property, which we can use to determine how many times we need to loop. Actually, as we know how many forms there are, we could just write the number in. However, here I'm demonstrating the length property, since it is then easier to add to the array without having to change the function. Generalizing your code like this is a good practice to get into.

The function starts by getting the number of Form objects within the forms array and stores it in variable numberForms.

function window_onload() { var numberForms = document.forms.length; Next we define a variable, formIndex, to be used in our for loop. After this comes the for loop itself. var formIndex; for (formIndex = 0; formIndex < numberForms; formIndex++) { alert(document.forms[formIndex].name); }

Remember that since the indices for arrays start at zero, our loop needs to go from an index of 0 to an index of numberForms – 1. We do this by initializing the formIndex variable to zero, and setting the condition of the for loop to formIndex < numberForms.

Within the for loop's code, we pass the index of the form we want (that is, formIndex) to document.forms[], which gives us the Form object at that array index in the forms array. To access the Form object's name property, we put a dot at the end and the name of the property, name.

Other Form Object Properties and Methods

The HTML elements commonly found in forms, which we will look at in more detail shortly, also have corresponding objects. One way of accessing these is through the elements[] property of the Form object. This is an array just like the forms[] array property of the document object that we have just seen. The elements[] array contains all the objects corresponding to the HTML interaction elements within the form, with the exception of the little used <INPUT TYPE=image> element. As we'll see later, this property is very useful for looping through each of the elements in a form. For example, we could loop through each element checking that it contains valid data prior to submitting the form.

Being an array, the elements[] property of the Form object has the length property, which tells us how many elements are in the form. The Form object also has the length property, which also gives us the number of elements in the form. Which of these you use is up to you since both do the same job, although writing document.myForm.length is shorter, and so quicker to type and less lengthy to look at in code, than document.myForm.elements.length.

When we submit data from a form to a server, we normally use the submit button, which we will come to shortly. However, the Form object also has the submit() method which does nearly the same thing. It differs in Netscape Navigator since it does not call the onsubmit event handler for the submit event of the Form object.

Recall that in the last chapter we saw how return values passed back from an event handler's code can affect whether the normal course of events continues or is cancelled. We saw, for example, that returning false from a hyperlink's onclick event handler causes the link's navigation to be cancelled. Well, the same principle applies to the Form object's onsubmit event handler, which fires when the user submits the form. If we return true to this event handler, then the form submission goes ahead; if we return false then the submission is cancelled. This makes the onsubmit event handler's code a great place to do form validation; checking that what the user has entered into the form is valid. For example, if we ask for their age and they enter "mind your own business" we can spot that this is text rather than a valid number, and stop them from continuing. We'll see this in action when we look at server-side scripting in Chapter 15.

As well as there being a reset button, which we will discuss later in the chapter, the Form object has the reset() method, which clears the form, or restores default values if these exist.

Creating blank forms is not exactly exciting or useful, so now let's turn our attention to the HTML elements that provide interaction functionality inside our forms.

home / progamming / javascript / beginning / chap6 / 1 12
[previous]

Copyright 1999 Wrox Press Ltd. and

JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES
webref The latest from WebReference.com Browse >
Perl Pragma Primer · Implement Drag and Drop in Your Web Apps: Part 2 · How to Create an Ajax Autocomplete Text Field: Part 5
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
SQL Server 2005 Express Edition - Part 22 - Upgrading from Microsoft SQL Server Desktop Engine (MSDE) · Vyatta: Downgrades that Pay Off · NetMotion Brings Cross-Network Support to Wireless VoIP


Created: January 22, 2001
Revised: January 22, 2001

URL: http://webreference.com/programming/javascript/beginning/chap6/1/