Example of .delegate() in jQuery
April 30, 2013
.delegate() in jQuery associates handlers and events. For any DOM element .delegate() can attach handler like click to any event like any user defined function.Syntax is
.delegate()
<!DOCTYPE html> <html> <head> <style> p { color:blue; cursor:pointer;} </style> <script src="http://code.jquery.com/jquery-1.9.1.js"></script> </head> <body> <p>Click to test delegate.</p> <script> var i=0; $("body").delegate("p", "click", function(){ i=i+1; $(this).after("<br/>"+i); }); </script> </body> </html>
Output