Can't you just put some marker on the list item and give them all the same event? Or, better yet, use a live()
event and then you don't even need to add one:
$('#myUL').append('<li id='a'>a</li>');
with:
$(function() {
$("#myUL li").live("click", function() {
if (this.id == "a") {
...
}
});
});
Otherwise just reverse the order and the syntax is much nicer:
$"<li></li>").attr("id", "a").text("a").appendTo("#myUL").click(function() {
...
});
You could simply do this:
$"<li id='" + a + "'>" + a + "</li>")..appendTo("#myUL").click(function() {
...
});
but I don't recommend this when dealing with dynamic input because you may not properly escape special characters whereas calling attr()
and text()
will correctly escape content and make you less susceptible to XSS exploits.