Tuesday, March 17, 2009

JQuery event functions do not work for dynamic added elements

$("a").click(function () {
alert("I was clicked");
});

<a href="http://www.blogger.com/post-edit.do">click me</a>

If you click the link, a window will popup. However, if you use JQuery dynamicly add some new <a> elements, they won't work with the click function. For example,

$("p").append("<a href='#'>I am added dynamically</a>"), and if you click the link, you will find that the function doesn't work at all.

No comments: