The fundamental thing is to hook the event on a container that you'll be adding the elements to. Most events bubble, and so (for instance) a click on a child element bubbles up to the parent element.
So say you have:
<ul id="container"></ul>
and you do this:
document.getElementById("container").addEventListener("click", function(e) {
console.log(e.target.innerHTML);
});
setInterval(function() {
var li = document.createElement('li');
li.innerHTML = "Added " + new Date();
document.getElementById("container").appendChild(li);
}, 2000);
Now we have new li
s being added every couple of seconds. Clicking the list item will show its contents, because the click bubbles to the parent container, which is where we've hooked the click. e.target
is a reference to the element where the event originated.
This is called event delegation.
Live Example | Source
If you have elements within the child elements, you may have to look at e.target.parentNode
or e.target.parentNode.parentNode
, etc., to find the one you care about. (This usually involves a loop, going to each parent node in turn, stopping when you've reached the element on which you hooked the event.)
Side note: Using jQuery is using JavaScript (unless you use CoffeeScript, TypeScript, Dart, or some other language). jQuery is a library, not a language.