Appending HTML w/click event using jQuery

html, jquery

Solution

What about :

$("#myID").click(
  function () {
     var someText = "my dynamic text";
     var newDiv = $("<div>").append(someText).click(function () { alert("click!"); });
     $(this).append(newDiv);
  }
)

Problem

I want to append some HTML inside this div, and the HTML has to have a click event on it also. ``` <div id="myID"> <p>hello, world!</p> </div> ``` I want to insert a new div inside of the above div, and I want a click event on the new HTML I insert there. There will be some dynamic text inside the new div so it has to by dynamically created. How can it be done?

Original source