Event Listener for Dynamically Created Elements

You can just register it in advance with .on(): Live demo (click).

$(document).on('click', 'some-selector', function() {


or create the dom element newTerm before appending it and attach the function directly: Live demo (click).

var $newTerm = $(newTerm);

$newTerm.click(function() {


Since the selector you’re adding to the element is a dynamic id, I’d recommend going with the second approach here. If you want to add a class to the generated elements so that you can delegate .on to them, use .on. It’s more efficient.

Leave a Comment