JavaScript
  Home arrow JavaScript arrow Page 2 - Event Delegation in JavaScript
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
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? 
Google.com  
JAVASCRIPT

Event Delegation in JavaScript
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2009-04-15


    Table of Contents:
  • Event Delegation in JavaScript
  • What you shouldn’t do with JavaScript event handlers
  • Taking advantage of JavaScript event delegation
  • The event delegation approach in action

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Event Delegation in JavaScript - What you shouldn’t do with JavaScript event handlers
    ( Page 2 of 4 )

    A good place to start demonstrating the real benefits to using JavaScript event delegation is with recreating the hypothetical situation discussed in the introduction, where one click handler is inefficiently assigned to each cell of an HTML table.  

    In this particular case, the purpose of doing this is to create a simple highlighting effect on all of the table cells. This example could be implemented with the following code:  

    <!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>Classic event handling with JavaScript</title>

    <script language="javascript">

    // highlight table cells when web page has been loaded

    window.onload=function(){

    if(document.getElementsByTagName&&document.getElementById&&document.createElement){

    // get target table

    var mytable=document.getElementById('mytable');

    if(!mytable){return};

    // get cells in target table

    var cells=mytable.getElementsByTagName('td');

    if(!cells){return};

    // assign 'onclick' event handler to each table cell

    for(var i=0;i<=cells.length;i++){

    cells[i].onclick=function(){

    this.className='highlighted';

    }

    }

    }

    }

    </script>

    <style type="text/css">

    table{

    width: 500px;

    border: 1px solid #000;

    }

    td{

    font: normal 10pt Arial, Helvetica, sans-serif;

    color: #000;

    background: #9fc;

    border: 1px solid #000;

    }

    .highlighted{

    background: #0c9;

    }

    </style>

    </head>

    <body>

    <h1>Classic event handling with JavaScript</h1>

    <table id="mytable">

    <tr>

    <td>This is the content of the cell</td>

    <td>This is the content of the cell</td>

    <td>This is the content of the cell</td>

    </tr>

    <tr>

    <td>This is the content of the cell</td>

    <td>This is the content of the cell</td>

    <td>This is the content of the cell</td>

    </tr>

    <tr>

    <td>This is the content of the cell</td>

    <td>This is the content of the cell</td>

    <td>This is the content of the cell</td>

    </tr>

    <tr>

    <td>This is the content of the cell</td>

    <td>This is the content of the cell</td>

    <td>This is the content of the cell</td>

    </tr>

    <tr>

    <td>This is the content of the cell</td>

    <td>This is the content of the cell</td>

    <td>This is the content of the cell</td>

    </tr>

    <tr>

    <td>This is the content of the cell</td>

    <td>This is the content of the cell</td>

    <td>This is the content of the cell</td>

    </tr>

    <tr>

    <td>This is the content of the cell</td>

    <td>This is the content of the cell</td>

    <td>This is the content of the cell</td>

    </tr>

    <tr>

    <td>This is the content of the cell</td>

    <td>This is the content of the cell</td>

    <td>This is the content of the cell</td>

    </tr>

    <tr>

    <td>This is the content of the cell</td>

    <td>This is the content of the cell</td>

    <td>This is the content of the cell</td>

    </tr>

    <tr>

    <td>This is the content of the cell</td>

    <td>This is the content of the cell</td>

    <td>This is the content of the cell</td>

    </tr>

    </table>

    </body>

    </html> 

    Despite its simplicity, the above example shows how to build a JavaScript-based highlighting effect by assigning a click handler to each cell of an HTML table. As you can see, every time a user clicks on an individual cell, a “highlight” CSS class will be tied to it, thus achieving the effect. 

    Logically, the down side to using this approach is that if the HTML table contains a large number of cells, then the browser will be obligated to process the same number of click handlers, which may seriously slow the performance of computers with a limited amount of RAM. 

    Okay, now that you've learned the “bad” way to work with JavaScript event handlers, it’s time to see how the previous example can be partially rebuilt by taking advantage of the functionality provided by event delegation.  

    To see how this brand new example will be developed, please click on the link that appears below and keep reading.  



     
     
    >>> More JavaScript Articles          >>> More By Alejandro Gervasio
     

       

    JAVASCRIPT ARTICLES

    - Introduction to JavaScript
    - Adding Elements to a Tree with TreeView jQue...
    - Using the Persist Argument in a TreeView jQu...
    - Using Unique and Toggle in a TreeView jQuery...
    - Using Event Delegation for Mouseover Events ...
    - Using the Animate Option in a Treeview jQuer...
    - Using HTML Lists with Event Delegation in Ja...
    - Opened and Closed Branches on a TreeView jQu...
    - Mouseover Events and Event Delegation in Jav...
    - Creating a TreeView JQuery Hierarchical Navi...
    - Event Delegation in JavaScript
    - A Look at the New YUI Carousel Control
    - Working with Draggable Elements and Transpar...
    - Displaying Pinned Handles with Resizable Con...
    - Building Resizable Containers with the Ext J...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek