Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • jQuery Tutorial
  • jQuery Selectors
  • jQuery Events
  • jQuery Effects
  • jQuery Traversing
  • jQuery HTML & CSS
  • jQuery AJAX
  • jQuery Properties
  • jQuery Examples
  • jQuery Interview Questions
  • jQuery Plugins
  • jQuery Cheat Sheet
  • jQuery UI
  • jQuery Mobile
  • jQWidgets
  • Easy UI
  • Web Technology
Open In App
Next Article:
Difference between on() and live() or bind() in jQuery
Next article icon

Difference between on() and live() or bind() in jQuery

Last Updated : 17 Dec, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report
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]);

Next Article
Difference between on() and live() or bind() in jQuery

D

devproductify
Improve
Article Tags :
  • Technical Scripter
  • Web Technologies
  • HTML
  • JQuery
  • jQuery-Misc
  • jQuery-Methods

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences