Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.live() vs .bind() [duplicate]

Tags:

jquery

bind

live

I want to know the main difference between

.live() vs. .bind()

methods in jQuery.

like image 219
Pranay Rana Avatar asked Apr 22 '10 11:04

Pranay Rana


People also ask

What is the difference between BIND and live?

bind() will only apply to the items you currently have selected in your jQuery object. . live() will apply to all current matching elements, as well as any you might add in the future.

What is the difference between bind () vs Live () vs delegate () methods in jQuery?

The bind() method will not attach events to those elements which are added after DOM is loaded while live() and delegate() methods attach events to the future elements also. The difference between live() and delegate() methods is live() function will not work in chaining.

Is bind deprecated?

bind() has been deprecated. It was superseded by the . on() method for attaching event handlers to a document since jQuery 1.7, so its use was already discouraged.

What is live event in jQuery?

The live() method attaches one or more event handlers for selected elements, and specifies a function to run when the events occur. Event handlers attached using the live() method will work for both current and FUTURE elements matching the selector (like a new element created by a script).


2 Answers

The main difference is that live will work also for the elements that will be created after the page has been loaded (i.e. by your javascript code), while bind will only bind event handlers for currently existing items.

// BIND example $('div').bind('mouseover', doSomething); // this new div WILL NOT HAVE mouseover event handler registered $('<div/>').appendTo('div:last');  // LIVE example $('div').live('mouseover', doSomething); // this new appended div WILL HAVE mouseover event handler registered $('<div/>').appendTo('div:last'); 

Update:

jQuery 1.7 deprecated live() method and 1.9 has removed it. If you want to achieve the same functionality with 1.9+ you need to use a new method on() which has slightly different syntax as it's invoked on document object and the selector is passed as a parameter. Therefor the code from above converted to this new way of binding events will look like this:

// ON example $(document).on('mouseover', 'div', doSomething); // this new appended div WILL HAVE mouseover event handler registered $('<div/>').appendTo('div:last'); 
like image 79
RaYell Avatar answered Oct 10 '22 19:10

RaYell


I did a statistical analysis of .bind() vs .live() vs .delegate() using FF profiler. I did 10 rounds of each (not a sufficient sample to be definitive, but illustrates the point). These are the results.

1) Single static element with an id using the click event:

.bind():     Mean = 1.139ms, Variance = 0.1276ms .live():     Mean = 1.344ms, Variance = 0.2403ms .delegate(): Mean = 1.290ms, Variance = 0.4417ms 

2) Multiple static elements with a common class using the click event:

.bind():     Mean = 1.089ms, Variance = 0.1202ms .live():     Mean = 1.559ms, Variance = 0.1777ms .delegate(): Mean = 1.397ms, Variance = 0.3146ms 

3) Multiple dynamic elements (first button creates second...) using the click event:

.bind():     Mean = 2.4205ms, Variance = 0.7736ms .live():     Mean = 2.3667ms, Variance = 0.7667ms .delegate(): Mean = 2.1901ms, Variance = 0.2838ms 

Interpret how you wish, but it seems to me that as dynamic elements increase on a page, .delegate() seems to have the best performance while static elements perform best with .bind().

Keep in mind that I am using a very simple click event triggering an alert. Different pages, with different environments (ie. CPU, multi-tab browsing, running threads, etc) will render differing results. I used this as a basic guideline for my decision to use one or the other. Please advise if you have come up with a different result.

Thanks!

like image 30
Watermark Studios Avatar answered Oct 10 '22 19:10

Watermark Studios