Skip to content

Instantly share code, notes, and snippets.

@qcam
Last active August 29, 2015 14:01
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 qcam/55942d4ec2ba6c420a25 to your computer and use it in GitHub Desktop.
Save qcam/55942d4ec2ba6c420a25 to your computer and use it in GitHub Desktop.
jQuery event binding

The markup

<div id="parent">
  <a id="child">Click here please</a>
</div>

Instead of binding event this way this way

$(".child").on("click", function() {
  alert("Child clicked!");
});

Bind the click event to parent so the event will always be trigger even if you replace the .child dom

$("#parent").on("click", ".child", function(e) {
  alert("Child clicked");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment