JavaScript
  Home arrow JavaScript arrow Page 2 - Working with IDs and Classes with the Beha...
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

Working with IDs and Classes with the Behaviour JavaScript Library
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 2
    2007-03-26

    Table of Contents:
  • Working with IDs and Classes with the Behaviour JavaScript Library
  • Extending the use of CSS selectors with the Behaviour library
  • Assigning JavaScript functions by using CSS classes
  • Using IDs and classes together

  • 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


    Working with IDs and Classes with the Behaviour JavaScript Library - Extending the use of CSS selectors with the Behaviour library


    (Page 2 of 4 )

    In consonance with the prerequisites that I stated right at the beginning of this article, I'm going to set up a practical example. In this example the Behaviour package is used to attach a custom JavaScript function to a specific CSS selector, in this case using only an ID attribute.

    Before you see the pertinent example, first let me show you how a hypothetical (X)HTML file would look, in this case using a few inline handlers associated with some containing DIVs.

    This poor implementation of inline handlers could be coded as follows:

    <!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 using inline event handler</title>
    <style type="text/css">
    h1{
      
    font: bold 24px Arial, Helvetica, sans-serif;
      
    color: #000;
    }
    h2{
      
    font: bold 16px Arial, Helvetica, sans-serif;
       
    color: #00f;
    }
    #header{
      
    height: 200px;
      
    background: #ffc;
      
    border: 1px solid #999;
    }
    #content{
      
    height: 400px;
      
    background: #f90;
      
    border: 1px solid #999;
    }
    #footer{
      
    height: 200px;
      
    background: #ffc;
      
    border: 1px solid #999;
    }
    </style>
    </head>
    <body>
      
    <h1>Example of using inline event handler</h1>
      
    <div id="header" onclick="alert('This is the header
    section.')"><h2>This is the header section</h2></div>
      
    <div id="content" onclick="alert('This is the content
    section.')"><h2>This is the content section</h2></div>
      
    <div id="footer" onclick="alert('This is the footer
    section.')"><h2>This is the footer section</h2></div>
    </body>
    </html>

    Definitely, you'll have to agree with me that the above example looks really ugly. It includes three inline JavaScript handlers, which have been attached to the DIVs that I mentioned before.

    However, all is not lost here. Below I recreated the same example, but in this case using the Behavior library. Naturally, the first thing to do consists of defining the set of rules that must be applied to a specific CSS selector. Here is the JavaScript file that accomplishes this:

    var rulediv={
      
    '#header' : function(element){
        
    element.onclick = function(){
          
    alert('This event handler has been assigned via the
    Behaviour library.');
         
    }
      
    }
    };
    Behaviour.register(rulediv);

    As you can see, the above JavaScript file defines a simple rule. This rule states that all the web page elements that have an ID attribute with a value of "header" will be associated to a primitive function, tasked with displaying a basic alert box on the browser.

    Now that you know how the previous CSS rule looks, please take a look the signature of the following (X)HTML file, which uses the JavaScript snippet that was defined earlier. Here is the corresponding definition:

    <!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 Behaviour JavaScript Library</title>
    <style type="text/css">
    h1{
      
    font: bold 24px Arial, Helvetica, sans-serif;
      
    color: #000;
    }
    h2{
       font: bold 16px Arial, Helvetica, sans-serif;
      
    color: #00f;
    }
    #header{
      
    height: 200px;
      
    background: #ffc;
      
    border: 1px solid #999;
    }
    #content{
      
    height: 400px;
      
    background: #f90;
      
    border: 1px solid #999;
    }
    #footer{
      
    height: 200px;
      
    background: #ffc;
      
    border: 1px solid #999;
    }
    </style>
    <script language="javascript" src="behaviour.js"></script>
    <script language="javascript" src="ruledivs.js"></script>
    </head>
    <body>
      
    <h1>Example using Behaviour JavaScript Library</h1>
      
    <div id="header"><h2>This is the header section</h2></div>
      
    <div id="content"><h2>This is the content section</h2></div>
      
    <div id="footer"><h2>This is the footer section</h2></div>
    </body>
    </html>

    Wasn't the previous file tighter and easier to code? Definitely it was. Of course, in this case, a basic JavaScript function is assigned to the first DIV, which is identified as "header," but the difference to spot here is that both the JavaScript code and the structural markup reside in different files.

    Okay, at this stage you already saw how to attach a JavaScript function to a specified CSS selector, using its ID attribute The next step to take consists of demonstrating how a similar process can be performed using CSS classes. Sounds pretty interesting, right?

    As usual, to see how this assignment of functions can be achieved by using the Behaviour package, please jump into the next section and keep reading. I'll be there, waiting for you.

    More JavaScript Articles
    More By Alejandro Gervasio


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

    JAVASCRIPT ARTICLES

    - 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...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget






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