What is a replacement for jQuery.live

What should be a proper jQuery.live () replacement. I know that jQuery.on () does the same, but in the case of jQuery.on () we have to specify the parent for it to work.

Suppose I have 500 live on my page, and now I want to change all 500 live to include and do this by selecting the parent element for each element, and this will take a lot of time.

Suppose that $(".item").live("click",function(){ alert("test"); });

and if I replaced it with jQuery.on (), then it should be $("body").on("click",".item",function(){ alert("test"); });

but I am wondering if there is any quick access method or something else to achieve this, for example

 $(".item").on("click",function(){ alert("test"); }); 

so that I can replace everything in a few seconds.

+6
source share
3 answers

.live() is dead, long live .live() : https://github.com/jquery/jquery/commit/eed78cc321ed7e2b37fb34e32fbb528b24f61d6e

Although seriously, for the single-line search you are looking for, you can simply add it back:

 jQuery.fn.live = function( types, data, fn ) { jQuery( this.context ).on( types, this.selector, data, fn ); return this; } 

Add JS to the top to efficiently create a padding that allows you to use .live() on your own and not care about which version of jQuery you are using.

But in reality, as others say, you just need to find find-replaceall using the text editor of your choice or a command line utility such as sed. In the puck solution that I give you, you call a function to call another function that introduces a lot of overhead and can use an unnecessary amount of resources. .on() much more efficient, especially if you call it directly.


Update . The above solution is based on the internal jQuery .context , which is deprecated since jQuery 1.10 and may disappear in any future version. An alternative version that should suit your needs most of the time is to simply replace this.context with document . This has the limitations described in . Context Properties API :

The value of this property is usually equal to the document, since this is the default context for jQuery objects, if none are specified. the context may differ if, for example, an object was searched in an <iframe> or XML document.

Thus, it may fail. Anyway, by now you have no excuse for not using .on() directly. Remember to check out my tips for upgrading to the latest version of jQuery .

+4
source

You can define a small plugin that provides you with the old live function by calling on , but that will only allow you with worse code. You need to reorganize your code to use on correctly.

A solution that helps you ease the initial change will be easier to use the regex for replacement. Most text editors can let you do this.

In case of use, you can use this (example in JavaScript, adapt for your editor):

 var output = input.replace( /\$\(\"([^\"]+)\"\)\.live\(\"([^\"]+)\"/g, '$(document.body).on("$2", "$1"' ); 

Entrance:

 $(".item").live("click",function(){ alert("test"); }); $(".item2").live("change",function(){ console.log("test"); }); 

Output:

 $(document.body).on("click", ".item",function(){ alert("test"); }); $(document.body).on("change", ".item2",function(){ window.top.console.log("test"); }); 

After this initial conversion, you will have to look for better parent elements than document.body .

+3
source

Like @destroy answer - something else to try - after backing up your file

I don't have Komodo, but I managed to develop the following for Notepad ++:

Find

 \$\("(.+)"\).live\("(.+)", 

Replace

 $(document).on('\2', '\1', 

The regex syntax is changing, so you may have to work on it.

You may also need to adapt or run several regex options, for example, to use single or double quotes or spaces.

+2
source

All Articles