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

68,958

Solution 1

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');
    })
})

Solution 2

.live() was introduced in jQuery 1.3, therefore it won't work with earlier versions.

.live() has also since been deprecated in jQuery 1.7 onwards.

The alternatives are .on() and .delegate()

See related question jQuery 1.9 .live() is not a function on how to migrate existing code.

I used "jquery-1.8.3.min.js" and my problem solved.

Solution 3

Try this out :- http://jsfiddle.net/trdb9/

JS:-

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

HTML:-

<a class="offsite">Click Me</a>

Solution 4

You need to use jquery-migrate-1.1.1.min.js or higher. I guess there are big changes coming in jquery, one being making everyone who relied on .live to find new ways.

Anyhow, try that and it should work.

Solution 5

Try replacing live with on in your code.

 $('.sm2_expander').on('click', function() {
    $(this).parent().parent().toggleClass('sm2_liOpen').toggleClass('sm2_liClosed');
    return false;
});
Share:
68,958
Enes Pekkaya
Author by

Enes Pekkaya

Updated on July 27, 2022

Comments

  • Enes Pekkaya
    Enes Pekkaya almost 2 years

    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

  • Enes Pekkaya
    Enes Pekkaya about 11 years
    Thank you for your response but it doesn't work. Example page is : boagworld.com/demos/sitemap
  • Enes Pekkaya
    Enes Pekkaya about 11 years
    Thank you for your response but it doesn't work. Example page is : boagworld.com/demos/sitemap
  • Aditya Singh
    Aditya Singh about 11 years
    Your demo site is using jQuery 1.3 version
  • Enes Pekkaya
    Enes Pekkaya about 11 years
    Thank you. That is my solution.