Skip to content

Instantly share code, notes, and snippets.

@ericanderson

ericanderson/index.html

Last active Dec 21, 2015
Embed
What would you like to do?
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