Chapter 18. Forms and Form Elements


As shown in examples throughout this book, HTML forms are an important component of many client-side JavaScript programs. This chapter explains the details of programming with forms in JavaScript. It is assumed that you are already somewhat familiar with the creation of HTML forms and with the input elements that they contain. If not, you may want to refer to a good book on HTML.[*]

[*] Such as HTML and XHTML: The Definitive Guide by Chuck Musciano and Bill Kennedy (O'Reilly).

If you are already familiar with server-side programming using HTML forms, you may find that things are done differently when forms are used with JavaScript. In the server-side model, a form with the input data it contains is submitted to the web server all at once. The emphasis is on processing a complete batch of input data and dynamically producing a new web page in response. With JavaScript, the programming model is quite different. In JavaScript programs, the emphasis is not on form submission and processing but instead on event handling. A form and all input elements in it have event handlers that JavaScript can use to respond to user interactions within the form. If the user clicks on a checkbox, for example, a JavaScript program can receive notification through an event handler and may respond by changing the value displayed in some other element of the form.

With server-side programs, an HTML form isn't useful unless it has a Submit button (or unless it has only a single text input field that allows the user to press the Enter key as a shortcut for submission). With JavaScript, on the other hand, a Submit button is never necessary (though it may still be useful). With JavaScript event handlers, a form can have any number of push buttons that perform various actions (including form submission) when clicked.

Examples throughout this book have also shown that event handlers are often the central element of a JavaScript program. Some of the most commonly used event handlers are those used with forms or form elements. This chapter introduces the JavaScript Form object and the various JavaScript objects that represent form elements. It concludes with an example that illustrates how you can use JavaScript to validate user input on the client before submitting it to a server-side program running on the web server.




JavaScript. The Definitive Guide
JavaScript: The Definitive Guide
ISBN: 0596101996
EAN: 2147483647
Year: 2004
Pages: 767

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net