TypeError: $(...).live is not a function

Enes Pekkaya picture Enes Pekkaya · Mar 22, 2013 · Viewed 68.4k times · Source

I have a problem with jquery 1.9.1 . I have searched it but these are not solved my problem.

    $('.sm2_expander').live('click', function() {
    $(this).parent().parent().toggleClass('sm2_liOpen').toggleClass('sm2_liClosed');
    return false;
});

Everybody said that "use 'on' function" but this time my code never work.

$(document).on("click", "a.offsite", function(){ alert("Goodbye!"); }); 

Edit : Here is my using prject page : draggable link

Answer

Arun P Johny picture Arun P Johny · Mar 22, 2013

In your example you have used the selector a.offsite but there are no elements matching this selector in your page. That might be the reason why it is not working.

$(function(){
    $(document).on('click', '.sm2_expander', function(){
        alert('bye');
        $(this).parent().parent().toggleClass('sm2_liOpen').toggleClass('sm2_liClosed');
    })
})

I think you can shorten this to

$(function(){
    $(document).on('click', '.sm2_expander', function(){
        $(this).closest('li').toggleClass('sm2_liOpen sm2_liClosed');
    })
})