JavaScript
  Home arrow JavaScript arrow Page 4 - Handling Forms, Events and More with the P...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Handling Forms, Events and More with the Prototype JavaScript Library
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 7
    2007-02-13

    Table of Contents:
  • Handling Forms, Events and More with the Prototype JavaScript Library
  • Iterator functions and event observers
  • Handling web forms
  • Using periodic and event-based form observers
  • Using the Element and Insertion objects

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Handling Forms, Events and More with the Prototype JavaScript Library - Using periodic and event-based form observers


    (Page 4 of 5 )

    If you found the previous "Form" and "Field" objects useful, I have more good news for you. Prototype comes packaged with a neat set of methods for observing online forms very easily.

    These form observers can be categorized in the following way. The first ones, called "periodic observers," can be used for determining that the data entered on a web form has changed during a specified period of time, while the second ones, dubbed "event-based observers," come in handy for verifying whether user-supplied input has varied after submitting the form.

    In both cases, observers can be attached to a complete form or a particular field, in this way implementing a robust mechanism for processing input data.

    Please study the following examples, which show how to use each one of the form observers that I explained previously. Here they are:

    (example using periodic form observer)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-
    8859-1" />
    <title>Example using periodic Form Observer</title>
    </head>
    <body>
     
    <form id="myform">
       
    First Name <input type="text" id="fname" /><br />
       
    Last Name <input type="text" id="lname" /><br />
       
    Email <input type="text" id="email" /><br />
       
    <input type="submit" name="send" value="Send Form" />
     
    </form>
     
    <script language="javascript" src="prototype-
    1.4.0.js"></script>
     
    <script language="javascript">
       
    // Example using periodic Form Observer
       
    var obs=new Form.Observer($("myform"),1,displayAlert);
       
    function displayAlert(){
           alert('Data has changed!');
       
    }
     
    </script>
    </body>
    </html>

    (example using event-based form observer)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-
    8859-1" />
    <title>Example using event-based Form Observer</title>
    </head>
    <body>
     
    <form id="myform">
       
    First Name <input type="text" id="fname" /><br />
       
    Last Name <input type="text" id="lname" /><br />
       
    Email <input type="text" id="email" /><br />
       
    <input type="submit" name="send" value="Send Form" />
     
    </form>
     
    <script language="javascript" src="prototype-
    1.4.0.js"></script>
     
    <script language="javascript">
       
    // Example using event-based Form Observer
       
    new Form.EventObserver($("myform"),displayAlert);
       
    function displayAlert(){
          alert('Data has changed!');
       
    }
     
    </script>
    </body>
    </html>

    (example using periodic field observer)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-
    8859-1" />
    <title>Example using periodic field observer</title>
    </head>
    <body>
     
    <form id="myform">
       
    First Name <input type="text" id="fname" /><br />
       
    Last Name <input type="text" id="lname" /><br />
       
    Email <input type="text" id="email" /><br />
       
    <input type="submit" name="send" value="Send Form" />
     
    </form>
     
    <script language="javascript" src="prototype-
    1.4.0.js"></script>
     
    <script language="javascript">
       
    // Example using periodic field observer
       
    new Form.Element.Observer($('fname'),1,displayAlert);
       
    function displayAlert(){
          alert('Data has changed!');
       
    }
     
    </script>
    </body>
    </html>

    (example using event-based field observer)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-
    8859-1" />
    <title>Example using event-based field observer</title>
    </head>
    <body>
     
    <form id="myform">
       
    First Name <input type="text" id="fname" /><br />
       
    Last Name <input type="text" id="lname" /><br />
       
    Email <input type="text" id="email" /><br />
       
    <input type="submit" name="send" value="Send Form" />
     
    </form>
     
    <script language="javascript" src="prototype-
    1.4.0.js"></script>
     
    <script language="javascript">
       
    // Example using periodic event-based field observer
       
    new Form.Element.EventObserver($('lname'),displayAlert);
       
    function displayAlert(){
          alert('Data has changed!');
       
    }
      </script>
    </body>
    </html>

    As shown above, periodic form and field observers are implemented via the "Form.Observer()" and "Form.Element.Observer()" methods respectively. These can be pretty useful in those cases where you wish to determine if user input has varied during a particular period of time.

    On the other hand, event-based form and field observers, like "Form.Element.Observer" and "Form.Element.EventObserver," can be very convenient for checking if input data has varied after a specific form has been submitted. Of course, it's probable that an adequate combination of the two types of observers can fit your particular needs, so keep this concept in mind when you're using Prototype for validating your own online forms.

    Okay, I believe that all the previous examples should give you a better idea of how to observe forms and fields with Prototype. So let's move on to the final section of this tutorial and learn how to use some additional objects included with this library and aimed at manipulating elements of a web page.

    More JavaScript Articles
    More By Alejandro Gervasio


       · Over the course of this last article of the series, you'll learn how to handle...
     

    JAVASCRIPT ARTICLES

    - More on JavaScript Array Objects
    - Methods of the DOM Location Object
    - The DOM Location Object Properties
    - Handling Remote Files with JavaScript Click ...
    - Using Click Interceptions with a Database-Dr...
    - Using JavaScript Click Interceptions in an I...
    - Using Click Interceptions with JavaScript
    - QuickSort in Action
    - Quicksort
    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...


     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT