The plain answer to your question: No, you can not do it. At least not in the new versions of jQuery (with 1.7 you can do it, as @undefined already states in the comments to the answer #15708467).
However, here's a simple example using delegation with .on():
$(function() {
$("#parent").on("click", ".child", function() {
alert($(this).text());
});
$("input").click(function() {
$(".child").remove();
$("#parent").append("<div class='child'>I'm a replacement</div>");
});
});
You'll notice after clicking the button, clicking the replacement div calls the same click function and alerts with the text of the new div.
Note: the important part here is that the replacement be selectable with the same selector (in this case .child
) as the original. I used a class here, but I could have used first child of the parent, or whatever else works.
Also note: You don't have to bind to an immediate parent either, you can bind all the way at the top with document
, but that's not recommended because it will slow down your code. You should bind as close to the elements you want to attach events to as possible so the code won't have to walk too far down the DOM tree.