estrar
estrar

Reputation: 1373

Delegate for jQuery 1.3.2

I got this code today that works for jQuery v7. The problem is that it need to work for 1.3.2 as well. How would I translate it?

$(document).ready(function() {
    $("ul#dropdown li ul").hide(); 
    $("ul#dropdown").delegate( 'li', 'click', function () {   
    if (!$(this).parent().is("ul#dropdown") ) {
            return false; 
        }  
    $(this).siblings('.' + "current").removeClass("current").children('ul').slideUp('fast');
    $(this).addClass("current").children('ul').slideDown('fast')
    });
});

Upvotes: 1

Views: 1074

Answers (3)

jAndy
jAndy

Reputation: 235992

The easiest way would be to replace .delegate() with .live() (live is in there since 1.3.0). This would not create the event handler on the ul node but on the document.body.

That means all events have to bubble further up, but unless you're dealing with tons of event handlers and nodes, there is no noticeable performance difference.

$("ul#dropdown li").live('click', function () { 
}

Ref.: .live()

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337560

The only issue I see is your use of delegate, which was added in 1.4. In 1.3.2 your only alternative is to use live():

$(document).ready(function() {
    $("ul#dropdown li ul").hide(); 
    $("ul#dropdown li").live('click', function () {   
        if (!$(this).parent().is("ul#dropdown") ) {
            return false; 
        }  
        $(this).siblings('.' + "current").removeClass("current").children('ul').slideUp('fast');
        $(this).addClass("current").children('ul').slideDown('fast')
    });
});

Upvotes: 0

Starx
Starx

Reputation: 78981

The only option for delegation in 1.3 is live() method

$("ul#dropdown li").live('click', function () {   
    if (!$(this).parent().is("ul#dropdown") ) {
            return false; 
        }  
    $(this).siblings('.' + "current").removeClass("current").children('ul').slideUp('fast');
    $(this).addClass("current").children('ul').slideDown('fast')
});

Upvotes: 2

Related Questions