Difference between on() and live() or bind() in jQuery
Last Updated : 17 Dec, 2021
jQuery offers various event handlers like
on(),
live() and
bind(). Though, there are some minor differences which are discussed below.
bind() method: This method only attaches events to elements which exist beforehand i.e. state of initialized document before the events are attached. If the selector condition is satisfied for an event afterward, bind() will not work on that function. It also won't work in the case if selector condition is removed from the element.
- Example: html
<!DOCTYPE html> <html> <head> <!-- CDN for jQuery --> <script src= "https://code.jquery.com/jquery-3.4.1.js"> </script> </head> <body> <div class="content"> <p class="a">This is Statement 1.</p> <script> /* Here, the bind() works on elements initialized beforehand only */ $(".a").bind("click",function(){ $(this).css("color","red"); }); </script> <p class="a">This is Statement 2.</p> <!-- click() method works on Statement 1 but not on Statement 2. --> </div> </body> </html>
- Output: Before clicking those statement:
After clicking those statement: 
live() method: This method attaches events not only to existing elements but also for the ones appended in the future as well but it won't work in the case if selector condition is removed from the element.
Note: The live() method was deprecated in jQuery version 1.7, and removed in version 1.9.
- Example: html
<!DOCTYPE html> <html> <head> <!-- Old CDN for .live() to work in jQuery --> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"> </script> </head> <body> <div class="content"> <p class="a">This is Statement 1.</p> <script> /* live() method works for elements appended later as well */ $(".a").live("click",function(){ $(this).css("color","red"); }); </script> <p class="a">This is Statement 2.</p> <!-- live() method works on both Statement 1 and Statement 2. --> </div> </body> </html>
- Output: Before clicking those statement:
After clicking those statement: 
on() method: This method attaches events not only to existing elements but also for the ones appended in the future as well. The difference here between on() and live() function is that on() method is still supported and uses a different syntax pattern, unlike the above two methods.
- Example: html
<!DOCTYPE html> <html> <head> <!-- CDN for jQuery --> <script src= "https://code.jquery.com/jquery-3.4.1.js"> </script> </head> <body> <div class="content"> <p class="a">This is Statement 1.</p> <script> /* Works on all elements within scope of the document */ $(document).on("click",".a",function(){ $(this).css("color","red"); }); </script> <p class="a">This is Statement 2.</p> <!-- on() method works on both Statement 1 and Statement 2. --> </div> </body> </html>
- Output: Before clicking those statement:
After clicking those statement: 
Differences summarized for the above methods: Property | bind() | live() | on() |
Deprecated | Yes | Yes | No |
Removed | No | Yes | No |
Scope | Elements initialized beforehand | For both current and future event binding | For both current and future event binding |
Syntax: | $([selector]).bind([event],[function]); | $([selector]).live([event],[function]); | $(document).on([event],[selector],[function]); |
Similar Reads
Difference between $(this) and 'this' in jQuery In this article, we will learn the difference between this and $(this) in jQuery. this keyword: In JavaScript, this keyword is used to refer to the object it belongs to. The value that this stores is the current execution context of the JavaScript program.Thus, when used inside a function this value
3 min read
Difference between hover() and mouseover() methods in jQuery Before learning the difference between the hover() and mouseover() method of jQuery, let's briefly see both methods. hover() Method: When we hover our mouse cursor to any element, two events happen i.e. mouseenter and mouseleave. mouseenter: When we bring the cursor over the element.mouseleave: When
3 min read
Difference between html() text() and val() methods in jQuery In this article, we are going to discuss the differences between html(), text(), and val() methods and their usage. 1. jQuery html() method: The html() method is used to set or return the inner HTML of a selected element. It works similarly to innerHTML in normal JavaScript to set or get the content
4 min read
What's the difference between selector and filter() in jQuery? jQuery Selectors: allow us to select and manipulate HTML element(s). It is used to select HTML elements such as ids, classes, types, attributes, etc using jQuery selectors and then add any CSS properties or events to the selected HTML elements. Syntax: For selecting a button tag the syntax would be
2 min read
What is the difference between eq() and get() methods in jQuery ? In this article, we will discuss all the differences between eq() and get() methods in jQuery. eq() Method: This method is used to locate the selected elements directly and returns an element with a specific index. Syntax: $(selector).eq(index) Example: In this example, we will set the different te
2 min read