I just appended some html to another JQuery object. I want to get a reference to the newly created dom node so that I can call another function on it. As seen in the code below, I am not getting a reference to what I wanted. Instead of the new node, I am getting the node with the original id.
var a = $("#id").append("some_html");
a.live('click', function(event){
alert("hello!");
});
It depends on what the HTML is. If it can be contained in a
divthen do this:A full example (using 1.4+ syntax) is this:
This difference is
appendreturns the parent object (it doesn’t break the existing chain), andappendToreturns the newly created object, even after it is appended.EDIT: Also, you are using
liveincorrectly. In this situation, just usebindor theclickhelper method:Best use: Finally, using jQuery 1.4+, you can just do this, no variable retention necessary: