What is the difference between `on` and `live` or `bind` in jQuery?


jQuery on() method

The on( events [, selector ] [, data ], handler ) method binds a handler to an event (like click) for all current − and future − matched element. It can also bind custom events.

Here is the description of all the parameters used by this method −

  • events − Event types separated by spaces.
  • selector − A Selector String
  • data − Data to be passed to the event handler in event.data
  • handler − A function to bind to the event on each of the set of matched elements

Example

You can try to run the following code to learn how to work with on() method −

<html>

   <head>
      <title>jQuery on() method</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
       
      <script>
         $(document).ready(function() {
            $('div').on('click', function( event ){
               alert('Hi there!');
            });
         });
      </script>
       
      <style>
         .div {
             margin:10px;
             padding:12px;
             border:2px solid #666;
             width:60px;
         }
      </style>
   </head>
   
   <body>
   
      <p>Click on any square below to see the result:</p>
       
      <div class = "div" style = "background-color:blue;"></div>
      <div class = "div" style = "background-color:green;"></div>
      <div class = "div" style = "background-color:red;"></div>
       
   </body>
</html>

Live Demo

jQuery live() method

The live( type, fn ) method binds a handler to an event (like click) for all current - and future - matched element. It can also bind custom events.

Here is the description of all the parameters used by this method −

  • type− An event type.
  • fn− A function to bind to the event on each of the set of matched elements

Example

You can try to run the following code to learn how to work with live() method in jQuery.

Live Demo

<html>
<head>
<title>jQuery live() method</title>
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
   <script>
   
   $(document).ready(function() {

     $('div').live('click', function( event ){
         alert('Hi there!');
     });

   });

   </script>
   <style>
      .div{
          margin:10px;
          padding:12px;
          border:2px solid #666;
          width:60px;
      }
  </style>
</head>
<body>
   <p>Click on any square below to see the result:</p>
   <div class="div" style="background-color:blue;"></div>
   <div class="div" style="background-color:green;"></div>
   <div class="div" style="background-color:red;"></div>
</body>
</html>

Note − The jQuery live() method deprecated in jQuery 1.7. It was finally removed in jQuery 1.9. To run the following program, use jQuery version before 1.7.

jQuery bind() method

The bind( type, [data], fn ) method binds a handler to one or more events (like click) for each matched element. It can also bind custom events.

Here is the description of all the parameters used by this method −

  • type − One or more event types separated by a space.
  • data − This is optional parameter and represents additional data passed to the event handler as event.data.
  • fn − A function to bind to the event on each of the set of matched elements.

Example

You can try to run the following code to learn how to work with bind() method −

Live Demo

<html>

   <head>
      <title>jQuery bind() method</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
       
      <script>
         $(document).ready(function() {
            $('div').bind('click', function( event ){
               alert('Hi there!');
            });
         });
      </script>
       
      <style>
         .div {
             margin:10px;
             padding:12px;
             border:2px solid #666;
             width:60px;
         }
      </style>
   </head>
   
   <body>
   
      <p>Click on any square below to see the result:</p>
       
      <div class = "div" style = "background-color:blue;"></div>
      <div class = "div" style = "background-color:green;"></div>
      <div class = "div" style = "background-color:red;"></div>
       
   </body>
</html>

Updated on: 17-Feb-2020

238 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements