JavaScript
  Home arrow JavaScript arrow Page 2 - Mouseover Events and 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

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


    Table of Contents:
  • Mouseover Events and Event Delegation in JavaScript
  • Review: JavaScript event delegation with click handlers
  • Enhancing event delegation with mouseover events
  • Putting it all together: a final (X)HTML file

  • 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


    Mouseover Events and Event Delegation in JavaScript - Review: JavaScript event delegation with click handlers
    ( Page 2 of 4 )

     

    Before I proceed to explain how to implement event delegation with “mouseover” events, let's recall how to use it with mouse clicks. I’m going to list the entire source code corresponding to the hands-one example developed in the preceding article of the series, in case you haven’t had the chance to read it. 

    That being said, here’s an (X)HTML file that implements event delegation with mouse clicks. Take a look at it, 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 on JavaScript event delegation (with click event)</title>

    <script language="javascript">

    // get target element

    function getEventTarget(e){

    var e=e || window.event;

    return e.target || e.srcElement;

    }

    // check if target is a table cell

    function highlightCell(e){

    var target=getEventTarget(e);

    if(target.tagName.toLowerCase()==='td') {

    target.className='highlighted';

    }

    }

    // run functions when web page has been loaded

    window.onload=function(){

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

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

    if(!mytable){return};

    // assign 'onclick' event handler to table (not cells)

    mytable.onclick=function(e){

    // determine target element and highlight table cell

    highlightCell(e);

    }

    }

    }

    </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>Event delegation in JavaScript (with click event)</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>

     

    As you can see, the above (X)HTML file demonstrates how useful event delegation can be for reducing the number of event handlers that must be assigned to certain elements of a web page. In this case, this approach is put to work with an HTML table to highlight its cells as they’re clicked on successively.  

    At first, it seems as if multiple click handlers had to be attached to the cells of the table, but thanks to the use of event delegation, only one was assigned to the table. On the other hand, the combination of the “getEventTarget()” and “highlightCell()” functions determined in which table cell the click occurred, by means of this event’s bubbling phase.  

    Now that you've recalled how the previous example functioned, it’s time to see how it can be partially rebuilt to work with “mouseover” events.  

    This topic will be discussed in detail in the section to come. Thus, click on the link shown 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 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek