Jquery Delegate Example - Jquery

How to use delegate method in jquery?

Snippet Code

Rate this page :
  [ 0 votes]

This jquery delegate allows you to attach one or more event to a parent element. When an event occurs the it specifies a function to run. Here,Clicking on the div adds another one. Note: delegate() attaches a click event handler to all divs - even new ones.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script> $(document).ready(function(){ $("body" ).delegate( "div", "click", function() { $( this ).after( "<div>Appended div element!</div>" ); }); }); </script> <div>This is a div element.</div>


Ask Questions

Ask Question