Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save thibaut-decherit/5f649c520a28e4398680a13fb9bfcc3c to your computer and use it in GitHub Desktop.
Save thibaut-decherit/5f649c520a28e4398680a13fb9bfcc3c to your computer and use it in GitHub Desktop.
jQuery - Selector supporting future DOM elements (nonexistent on document load)

jQuery - Selector supporting future DOM elements (nonexistent on document load)

Also supports DOM elements existent on document load. Potentially heavier on performance than "classic" selector.

Selector

/*
Used instead of $('.my-element').click(function (e) {} to be able to listen to click on elements with my-element class
created by JS insertion into DOM after document load.
*/
body.on('click', '.my-element', function (e) {
  // [...] Your code.
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment