The other day I've started a discussion on the jQuery dev list about event delegates and how the live() function can be improved with a few little tweaks from Zach Leatherman Blog - Performance Caveat with jQuery Selectors and Live Events

After reading at Zach's Blog I've was able to create a little plugin that I think will also be useful to those who are looking for an alternative way to bind live events to an element without first querying the document.


    $('#sidebar').delegate('li :click',callback); 

From my test in FF 3.5 and IE7 I was able to bind an event to a ul element with over 800 li elements with excellent performance!

Here's how to use the plugin:

    $(context).delegate('click',callback)  // same as live()

    $(context).delegate('li:even, li:odd :click',callback)  // supports multiple selectors

    $(context).delegate('li :click.namespace',callback)  // supports event namespace

    $(context).delegate('li:first a :click',callback)  // supports pseudo classes 

    $(context).delegate('li a :click mouseover',callback) // support future multiple event format 

The delegate method will use the same rules as that of the live() method.

Note: jQuery live() only supports single events: Unlike .bind(), only a single event can be bound in each call to the .live() method.

Here's the plugin code:

    <script type="text/javascript">
        jQuery.fn.delegate = function(event,fn) {
            var e,p,s;
            if (event.indexOf(':')) {
                s = event.substr(0,event.lastIndexOf(':'));
                event = event.replace(s+':','');
            if (!s) e = this;
            else {
                p = this.selector + ' ';
                if (!s.indexOf(',')) s = p + s;
                else s = p+(s.split(/,/)).join(','+p);
                e = jQuery(document); e.selector = s
            return this;

Working example:

    <script type="text/javascript">
            $('#sidebar').delegate('li :mouseover',cb);
            $('#sidebar').delegate('li :mouseout',cb);
            //$('#sidebar').delegate('li:first, li:last :click',cb);

        function cb (e){
            var c = (e.type=='mouseout') ? '#fff':'#ffcc00';

    <div id="sidebar">
            <li>item 1</li>
            <li>item 2</li>
            <li>item 3</li>
            <li>item 4</li>
            <!-- copy and paste more li elements here -->
nike air jordan 5 retro online kopen
Posts: 20
Reply #8 on : Thu December 20, 2018, 01:02:04
<a href="">nike negras 2016</a><a href="">nike air huarache 3 homme blanc chaussures</a><a href="">teris nike air max plus tn hombre cargando zoom</a><a href="">air max bw classic noir grant fr 1</a>
[url=]nike air jordan 5 retro online kopen[/url]
navy pink air revolution sky wmns wedge 599410 400 nike
Posts: 20
Reply #7 on : Wed December 26, 2018, 20:15:19
<a href="">bleu gris blanc enfant nike air max 90 gamma blue junior baskets discount</a><a href="">nike air jordan 6 retro high heels blackwhite kopen nike jordan high heel</a><a href="">nike free nano zalando</a><a href="">adidas neo caflaire sneaker herren wei? dunkelgrau</a>
[url=]navy pink air revolution sky wmns wedge 599410 400 nike[/url]
adidas 3st 002 pk
Posts: 20
Reply #6 on : Fri January 04, 2019, 20:33:27
<a href="">billigt sko herr sneakeradidas seeley skor nej 8537</a><a href="">nike air force 1 low nike air force 1 lo blackblack fs3gm</a><a href="">moda nike janoski hombre venta banana623</a><a href="">nike huarache run damen g?nstig</a>
[url=]adidas 3st 002 pk[/url]
material?berlegenheit adidas stan smith trainers off white p
Posts: 20
Reply #5 on : Fri February 08, 2019, 23:24:19
<a href="">ah8050 401 nike air max 270 blau herren freizeitschuhe</a><a href="">calidad barato hombres nike hyperdunk 2016 team golddark greywhite</a><a href="">nike air max 2017 dark blue gray mens best nike shoes 576</a><a href="">nike huarache come nuove numero 40 ma vestono molto meno a</a>
[url=]material?berlegenheit adidas stan smith trainers off white p[/url]
hombre nike roshe one zapatos casuales caqui avena blanco 511881 203
Posts: 20
Reply #4 on : Sat March 23, 2019, 06:30:59
<a href="">nike air jordan 4 retro levis white levis tag dame</a><a href="">nike air max 90 hoch tops schuhe m?nner alle wei? schuhe</a><a href="">nike jr hypervenom phantom iii academy fg uomo</a><a href="">puma glyde hi polka dot m?skie</a>
[url=]hombre nike roshe one zapatos casuales caqui avena blanco 511881 203[/url]
nike womens grey white pink air max tn
Posts: 20
Reply #3 on : Fri March 29, 2019, 08:55:18
<a href="">air max 180 black orange</a><a href="">find nike air max 90 vt mens sneakers army camo</a><a href="">exciting air max 97 nike black gray moon branch shoes</a><a href="">buy womens nike air max motion shoes deep grey green outlet</a>
[url=]nike womens grey white pink air max tn[/url]
nike zoom lebron soldier 10 id
Posts: 20
Reply #2 on : Tue April 02, 2019, 10:18:34
<a href="">nike free 3.0 v3 mens black red shoes</a><a href="">nike kwazi basketball shoes mens night maroon black vr</a><a href="">real nike air jordan 1 high black cement white varsity red 839115 013</a><a href="">mens nike air vapormax plus cool grey shoes</a>
[url=]nike zoom lebron soldier 10 id[/url]
mens nike air max 95 ultra essential blue shoes
Posts: 20
Reply #1 on : Wed April 10, 2019, 06:18:51
<a href="">nike zoom air generation pas cher</a><a href="">nike roshe run arancione</a><a href="">nike air force xxv baratas</a><a href="">nike air pegasus 89 wit</a>
[url=]mens nike air max 95 ultra essential blue shoes[/url]
First | Previous | Showing comments 11 to 18 of 18

Leave a Comment

Required fields are marked with *.
If you have trouble reading the code, click on the code itself to generate a new random code.