Skip to content

Instantly share code, notes, and snippets.

@d3netxer
Last active April 14, 2017 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d3netxer/bedef93488183f4dec248452e81b944f to your computer and use it in GitHub Desktop.
Save d3netxer/bedef93488183f4dec248452e81b944f to your computer and use it in GitHub Desktop.
jquery tip: attach listener to body tag

jquery tip: attach listener to body tag

$("body").on("click", "a", function(event) {
... your custom code
.... this attaches the listener to the body, but will match against any click from an <a> tag.
.... so you don't have to attach to every a tag every time, but can use only 1 listener
... also good, because you can attach the listener to a parent element and not have to update the listners whenever a new <a> tag is created
});

description $("<SELECTOR to attach to>").on("< event name>", "<selector for event sources>", <your function handler for that event >);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment