JavaScript
  Home arrow JavaScript arrow Page 2 - Introducing the Prototype JavaScript Libra...
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

Introducing the Prototype JavaScript Library
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2007-01-30

    Table of Contents:
  • Introducing the Prototype JavaScript Library
  • Handling specific elements of a web page: introducing the $ function
  • Working easily with arrays and hashes: using the $A and $H functions
  • Dealing easily with form fields: using the $F function

  • 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


    Introducing the Prototype JavaScript Library - Handling specific elements of a web page: introducing the $ function


    (Page 2 of 4 )

    One of the most useful functions that comes bundled with Prototype is undoubtedly the $ function. Despite its short name, this handy function allows you to retrieve a specific element of a web page by passing to it the corresponding ID attribute (this is similar to the "getElementById" method that belongs to the DOM).

    However, this function is much more flexible. It accepts multiple arguments, which means that it's possible to work with many elements included in same web document.

    Having outlined generally how the $ function works, let me show you a pair of brief examples, where single and multiple parameters are passed to the function in question. Here are the respective code samples: 

    The $ function using one parameter

    <!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 of $ function</title>
    <script language="javascript" src="prototype-1.4.0.js"></script>
    <script language="javascript">
    // example using the $ function with one element
    function displayHTML(){
                var div=$('samplediv');
                var htmlcont=$('htmlcontainer');
                htmlcont.innerHTML=div.innerHTML;
    }
    window.onload=function(){
                if(document.getElementById && document.createElement
    && document.getElementsByTagName){
                   var btn=$('testbutton');
                   if(!btn){return};
                   btn.onclick=displayHTML;
                }
    }
    </script>
    </head>
    <body>
      <div id="samplediv">
        <p>This is a sample paragraph, which is wrapped by a DIV</p>
      </div>
      <div id="htmlcontainer"></div>
      <input type="button" value="Show innerHTML" id="testbutton" />
    </body>
    </html>

    The $ function using three parameters

    <!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 of $ function</title>
    <script language="javascript" src="prototype-1.4.0.js"></script>
    <script language="javascript">
    // example using the $ function with multiple arguments
    function displayHTML(){
       var divs=$('samplediva','sampledivb','sampledivc');
       for(var i=0;i<divs.length;i++){
           var htmlcont=$('htmlcontainer');
           htmlcont.innerHTML+=divs[i].innerHTML;
       }
    }
    window.onload=function(){
       if(document.getElementById && document.createElement &&
    document.getElementsByTagName){
         var btn=$('testbutton');
         if(!btn){return};
         btn.onclick=displayHTML;
       }
    }
    </script>
    </head>
    <body>
     
    <div id="samplediva">
       
    <p>This is a sample paragraph, which is wrapped by the first
    DIV</p>
     
    </div>
     
    <div id="sampledivb">
       
    <p>This is a sample paragraph, which is wrapped by the
    second DIV</p>
     
    </div>
     
    <div id="sampledivc">
       
    <p>This is a sample paragraph, which is wrapped by the third
    DIV</p>
     
    </div>
     
    <div id="htmlcontainer"></div>
       
    <input type="button" value="Show innerHTML" id="testbutton" />
    </body>
    </html>

    As shown above, the first example demonstrates a simple implementation of the $ function with only one argument to first retrieve a "samplediv" DIV element, and then manipulate its "innerHTML" property. As you saw, the process is indeed simple and straightforward.

    The second example is slightly more complex. It shows how the $ function can be used for retrieving multiple elements of the same web document. In this case, three DIVs are fetched, and then their "innerHTML" property is also modified in a simple way.

    In addition, you should notice how the $ function returns an array of elements when called with multiple parameters. This feature places it several steps ahead of the conventional "getElementById()" method that corresponds to the DOM. Now do you see how handy this function can be for manipulating diverse web page elements?

    Based upon the two concrete examples that you saw before, I think that you should have a good idea of how the $ function does its thing. But I'm only scratching the surface when it comes to exploring the diverse possibilities offered by this cool function.

    Nevertheless, the Prototype library has many other useful functions, so it's time to move on and take a quick look at some of them. In the next section I'll be explaining how to use two handy functions that come bundled with this library: the $A and $H functions respectively.

    To learn how these functions can be used successfully, please click on the link below and keep reading.

    More JavaScript Articles
    More By Alejandro Gervasio


       · This first article of the series is focused on exploring the main features of this...
       · just a small correction - prototypes official site is...
       · Thank you commenting on this article and for the update on Prototype's official...
     

    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