JavaScript
  Home arrow JavaScript arrow Page 3 - 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 - Assigning JavaScript functions by using CSS classes


    (Page 3 of 4 )

    As you may have guessed, assigning a JavaScript function to a specified web page element via its class attribute is a process that looks very similar to the one performed when utilizing IDs. As I did with all the examples that I developed in the previous section, first I'll show you the "inline" version of the process in question, and then I'll show you the same example using the Behaviour library.

    Having said that, here is how to attach an alert box to a paragraph whose class attribute is defined with a value of "largepar." The corresponding code listing is 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;
    }
    .largepar{
      
    font: bold 20px Arial, Helvetica, sans-serif;
      
    color: #00f;
    }
    </style>
    </head>
    <body>
      
    <h1>Example of using inline event handler</h1>
      
    <p class="largepar" onclick="alert('This paragraph has an
    inline event handler.')">This paragraph has an inline event
    handler.</p>
    </body>
    </html>

    Pretty ugly, isn't it? Now that you saw the bad implementation of an inline handler which has been attached to the previous paragraph, have a look at the following example using the Behaviour library:

    (definition of "rulepar.js file)

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

    <!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 </title>
      
    <style type="text/css">
      
    h1{
        
    font: bold 24px Arial, Helvetica, sans-serif;
        
    color: #000;
       }
       .largepar{
        
    font: bold 20px Arial, Helvetica, sans-serif;
        
    color: #00f;
      
    }
      
    </style>
      
    <script language="javascript" src="behaviour.js"></script>
      
    <script language="javascript" src="rulepar.js"></script>
    </head>
    <body>
      
    <h1>Example using Behaviour JavaScript Library</h1>
      
    <p class="largepar">The event handler of this paragraph has been assigned via the Behaviour library.</p>
    </body>
    </html>

    As I said before, assigning a JavaScript function to a particular web page element via its class attribute is really a straightforward process, which can be easily performed using Behavior. And best of all, the respective structural and behavioral layers of the web document remain completely independent from each other at all times.

    So far, so good. At this point, you hopefully learned how to attach a single JavaScript function to a particular web page element via its ID or class attribute. Nonetheless, the Behaviour package really starts to shine when you work with parent and child tags, since it's possible to define a rule (or a set of them) that matches any CSS selector, no matter how its associated element is positioned within a given web document.

    This concrete case will be examined in the section to come, thus if you want to learn how this will be achieved, please keep reading.

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