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 −
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>
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 −
You can try to run the following code to learn how to work with live() method in jQuery.
<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.
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 −
You can try to run the following code to learn how to work with bind() method −
<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>