Skip to content

Instantly share code, notes, and snippets.

@ericanderson
Last active December 21, 2015 04:29
Show Gist options
  • Save ericanderson/6249790 to your computer and use it in GitHub Desktop.
Save ericanderson/6249790 to your computer and use it in GitHub Desktop.
Demonstrates preventDefault vs stopPropagation
<html>
<head>
<script src="//code.jquery.com/jquery-1.10.1.min.js"></script>
</head>
<body>
<form id="preventDefault">
<button>preventDefault</button>
</form>
<form id="stopPropagation">
<button>stopPropagation</button>
</form>
<form id="both">
<button>both</button>
</form>
<script>
$('form').click(function(e){
alert('in form');
});
$('#preventDefault button').click(function(e){
alert('in button');
e.preventDefault();
});
$('#stopPropagation button').click(function(e){
alert('in button');
e.stopPropagation();
});
$('#both button').click(function(e){
alert('in button');
e.preventDefault();
e.stopPropagation();
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment