JavaScript
  Home arrow JavaScript arrow Page 2 - 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 - Iterator functions and event observers


    (Page 2 of 5 )

    As I explained in the introduction, Prototype allows us to work easily with iterator functions. This concept can be applied to any traversable structure, including (x)HTML collections and array objects. However, if you take the time to consult the official documentation, you'll see that array objects expose an impressive set of functions which let you perform all sort of clever things, such as searching for and finding array elements, filtering them, converting arrays to strings, and so forth.

    In this specific case, I'm going to show you only a basic example of how to use the "find()" function. This will give you a clear idea of how to use iterator functions.

    Having said that, take a look at the following code sample. It demonstrates how to find a specific DIV element via its corresponding ID attribute. Here's the example in question:

    <!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 searching an element of an array with 'find()' function</title>
    <script language="javascript" src="prototype-1.4.0.js"></script>
    <script language="javascript">
    // example searching an array element with 'find()' function
    function findDivId(id){
       var divs=$A(document.getElementsByTagName('div'));
       var divobj=divs.find(function(div){return(div.id==id)});
       alert(divobj.id);
    }
    window.onload=function(){
       if(document.getElementById && document.createElement &&
    document.getElementsByTagName){
         var btn=$('testbutton');
         if(!btn){return};
         btn.onclick=function(){
             findDivId('divb');
         }
       }
    }
    </script>
    </head>
    <body>
     
    <div id="diva">
       
    <p>This is a sample paragraph, which is wrapped by the first
    DIV</p>
     
    </div>
     
    <div id="divb">
       
    <p>This is a sample paragraph, which is wrapped by the
    second DIV</p>
     
    </div>
     
    <div id="divc">
       
    <p>This is a sample paragraph, which is wrapped by the third
    DIV</p>
     
    </div>
     
    <input type="button" value="Show function result"
    id="testbutton" />
    </body>
    </html>

    As shown above, the brand new "find()" function is used here to find a DIV element within a sample web document, which is identified as "divb," in this way applying the concept of "iterator function." With reference to the previous example, you should also notice how all the respective DIVs are first converted into an array object via the $A function before being processed via "find()." Quite simple, right?

    So far, so good. At this stage you hopefully grasped the logic that stands behind the concept of iterator function. Bearing in mind this, let's move forward and see how to use the Prototype library to work with events.

    Speaking more specifically, you can utilize the helpful "Event.observe()" method to assign a concrete event to one element of a web page, and at the same time, specify a callback function to be invoked either in the capture or the bubble phase.

    The following example shows how to assign an "onclick" handler to all the DIVs contained into a sample web document by using the "Event,observe()" method. Take a look at the code listing, please:

    <!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 the Event object and 'observe()'
    method</title>
    </head>
    <body>
     
    <div id="diva">
       
    <p>This is a sample paragraph, which is wrapped by the first
    DIV</p>
      
    </div>
     
    <div id="divb">
       
    <p>This is a sample paragraph, which is wrapped by the
    second DIV</p>
      
    </div>
     
    <div id="divc">
       
    <p>This is a sample paragraph, which is wrapped by the third
    DIV</p>
     
    </div>
     
    <script language="javascript" src="prototype-
    1.4.0.js"></script>
     
    <script language="javascript">
       
    //Example using the Event object and 'observe()' method
       
    function displayDivId(){
           var divs=$A(document.getElementsByTagName('div'));
           divs.each(function(div){
              Event.observe(div,'click',function(){alert(div.id)}); 
           });
        }
        displayDivId();
      </script>
    </body>
    </html>

    After studying the previous example, I'm pretty sure that you'll realize how easy it is to assign diverse event handlers to the elements of a web page with Prototype, without having to deal with browser incompatibilities.

    You should practice using the "Event.observe()" method to assign different handlers to a range of elements included into a web document. In this way you will learn more quickly how to get the most of this handy feature. 

    Okay, at this point you learned how to work with events and the Prototype framework. Considering that there are still many important methods and functions included with this library that remain uncovered, in the following section I'm going to teach you how to use a couple of objects for processing online forms.

    Want to see how these brand new objects will be utilized? Keep reading, please.

    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 5 hosted by Hostway
    Stay green...Green IT