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

0

Good afternoon,

Clicking on a tab in my project returns the error: Uncaught TypeError: $ (...). Live is not a function.

Code:

function menu() {
    $('.nav-toggle').live('click', function() {
        if($(".nav-aberta").hasClass("side-fechado")) {
            $('.nav-aberta').animate({
                left: "0px",
            }, 100, function() {
                $(".nav-aberta").removeClass("side-fechado");
            });
        }
        else {
            $('.nav-aberta').animate({
                left: "-250px",
            }, 100, function() {
                $(".nav-aberta").addClass("side-fechado");
            });
        }
    });     
}

function redo () {     var sizeJanela = $ (window) .width ();     // $ ('. nav-toggle'). hide ();

if (tamanhoJanela < 800) { 
    $('.nav-aberta').css('left', '-250px').addClass('side-fechado');
    $('.nav-toggle').css('display', 'initial');
} else {
    $('.nav-aberta').css('left', '0px').addClass('side-fechado');
}   
menu();

}

//Menu Sidebar
$(window).resize(function() {
    refazer();
});

$(window).load(function() {
    refazer();
}); 

$(document).ready(function() {
    refazer();
});

Can anyone tell me what it can be? Thanks in advance.

    
asked by anonymous 26.05.2017 / 21:00

2 answers

2

Check the version of your jquery because .live was deprecated in version 1.7 and removed from version 1.9 onwards.

Source: link

Tip: If your version is 1.7 or higher, you can use .on () as an alternative, eg

$('.nav-toggle').on('click', function() {
    if($(".nav-aberta").hasClass("side-fechado")) {
        $('.nav-aberta').animate({
            left: "0px",
        }, 100, function() {
            $(".nav-aberta").removeClass("side-fechado");
        });
    }
    else {
        $('.nav-aberta').animate({
            left: "-250px",
        }, 100, function() {
            $(".nav-aberta").addClass("side-fechado");
        });
    }
});     
    
26.05.2017 / 21:03
0

The use of $('.nav-toggle').on('click', function() { is not equivalent to using $('.nav-toggle').live('click', function() { of the old API.

In other words, the .live tagged when the DOM was modified applied the events again, so much so that the link itself link that the other answer points out exactly how it should be done:

$( >>>>SELECTOR<<< ).live( events, data, handler );                // jQuery 1.3+
$( document ).delegate( >>>>SELECTOR<<<, events, data, handler );  // jQuery 1.4.3+
$( document ).on( events, >>>>SELECTOR<<<, data, handler );        // jQuery 1.7+

In other words, if you use version 1.4.3 until 1.6 you should use it like this:

function menu() {
    $(document).delegate('.nav-toggle', 'click', function() {
        if($(".nav-aberta").hasClass("side-fechado")) {
            $('.nav-aberta').animate({
                left: "0px",
            }, 100, function() {
                $(".nav-aberta").removeClass("side-fechado");
            });
        }
        else {
            $('.nav-aberta').animate({
                left: "-250px",
            }, 100, function() {
                $(".nav-aberta").addClass("side-fechado");
            });
        }
    });     
}

Now using versions 1.7 to current (version 3) you should use .on , like this:

function menu() {
    $(document).on('.nav-toggle', 'click', function() {
        if($(".nav-aberta").hasClass("side-fechado")) {
            $('.nav-aberta').animate({
                left: "0px",
            }, 100, function() {
                $(".nav-aberta").removeClass("side-fechado");
            });
        }
        else {
            $('.nav-aberta').animate({
                left: "-250px",
            }, 100, function() {
                $(".nav-aberta").addClass("side-fechado");
            });
        }
    });     
}
    
17.08.2018 / 00:58