Case1: List is present in the html already
- One
- Two
The cl
Assuming that you've got jQuery correctly loaded, and that you put your jQuery related code in a "document ready" handler, I'd recommend "event delegation", such that you just register a single event handler on a static ancestor element.
Clicks received in the li
descendants will "bubble up" to the ancestor element, but jQuery will ensure that this
is set to the element that was actually clicked:
$('#testDiv').on('click', 'li', function() {
console.log(this);
});
So long as the static ancestor remains present on the page you can dynamically change its contents as much as you please and the event handler will continue to work.