TypeError: $(...).live is not a function
Asked Answered
H

6

9

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

Hosanna answered 22/3, 2013 at 15:7 Comment(3)
The on version of $(".sm2_expander").live("click",func) is $(document).on("click",".sm2_expander",func). Don't know where you got "a.offsite".Rocker
can you share the html alsoJoub
need more info. Probably use jsfiddle.net to explain the problem.Vinegar
J
15

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');
    })
})
Joub answered 22/3, 2013 at 15:43 Comment(1)
Thank you. That is my solution.Hosanna
A
9

.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.

Aletheaalethia answered 28/7, 2014 at 12:0 Comment(0)
D
6

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

JS:-

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

HTML:-

<a class="offsite">Click Me</a>
Donella answered 22/3, 2013 at 15:21 Comment(2)
Thank you for your response but it doesn't work. Example page is : boagworld.com/demos/sitemapHosanna
Your demo site is using jQuery 1.3 versionDonella
E
3

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.

Epigenesis answered 27/5, 2013 at 20:11 Comment(0)
K
2

Try replacing live with on in your code.

 $('.sm2_expander').on('click', function() {
    $(this).parent().parent().toggleClass('sm2_liOpen').toggleClass('sm2_liClosed');
    return false;
});
Khosrow answered 22/3, 2013 at 15:17 Comment(1)
Thank you for your response but it doesn't work. Example page is : boagworld.com/demos/sitemapHosanna
I
0

If you have a live site, I will recommend to use jQuery Migrate

https://github.com/jquery/jquery-migrate/

It will automatically add the deprecated but needed functions.

Iridic answered 7/5, 2015 at 7:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.