1

Jquery delegate() deprecated , then how to listen dynamic create object event

FOR Example

$(document).ready(function() {
  $(document).find("p").on("click", function(event) {
    $("#contain").append("<p>Click this paragraph </p>" + event.timeStamp);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Click this paragraph.</p>
<div id="contain"></div>

Here contain>p click event not happened with on event delegate is working is there any idea for this instead of delegate() or live() ?

guradio
  • 15,524
  • 4
  • 36
  • 57

2 Answers2

2

You have used wrong syntax, Correct syntax is:

.on( events [, selector ] [, data ], handler )

Which makes your code:

$(document).on("click","p", function(event) {
  $("#contain").append("<p>Click this paragraph </p>" + event.timeStamp);
});
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
1
  1. Delegate like this.

$(document).ready(function() {
  $(document).on("click","p", function(event) {
    $("#contain").append("<p>Click this paragraph </p>" + event.timeStamp);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Click this paragraph.</p>
<div id="contain"></div>
guradio
  • 15,524
  • 4
  • 36
  • 57