Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jquery

live

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

like image 650
Enes Pekkaya Avatar asked Mar 22 '13 15:03

Enes Pekkaya


People also ask

What is .live in jQuery?

jQuery | live() MethodThis method is used to attach one or more event handlers for the selected elements. It also specifies the function that runs when the event occurs. The event handlers used will work for both current and future elements matching the selector.

Is not a function Typeerror is not a function?

This is a standard JavaScript error when trying to call a function before it is defined. This error occurs if you try to execute a function that is not initialized or is not initialized correctly. This means that the expression did not return a function object.

Is not a function WordPress error?

$ is not a function WordPress error occurs when the code comes before the jQuery library. For example, if a plugin or theme calls a code before calling the right library, you get this error. By default, WordPress doesn't understand $ as jQuery and you have to make some modifications to fix this error.

Is not a function error jQuery?

on if not a function" jQuery error occurs for 2 main reasons: Loading an old version of the jQuery library that doesn't support the on function. Loading a library that overrides the value of the dollar sign $ variable.


2 Answers

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

like image 51
Ipsita Rout Avatar answered Oct 20 '22 20:10

Ipsita Rout


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');
    })
})
like image 42
Arun P Johny Avatar answered Oct 20 '22 20:10

Arun P Johny