JQuery – Use The “On()” Method Instead Of “Live()”

As of jQuery 1.7, the .live() method is deprecated.Use.on() to attach event handlers.

Description: Attach an event handler function for one or more events to the selected elements.
The .on() method attaches event handlers to the currently selected set of elements in the jQuery object.

Syntax:-
.on(events[,selector][,data],handler(eventObj))
.on(eventType, selector, function)

Examples:-
$(“body”).on(“click”, “#element”, function(){
$(“#my”).html(result);
});

$(“body”).on(“click” , “p” ,function(){
alert($(this).text());
});

Migration from .live() to .on()
before:
$(‘#mainmenu a’).live(‘click’, function)
after, you move the child element (a) to the .on() selector:
$(‘#mainmenu’).on(‘click’, ‘a’, function)



Leave a Reply

2 × one =