Last active
December 21, 2015 04:29
-
-
Save ericanderson/6249790 to your computer and use it in GitHub Desktop.
Demonstrates preventDefault vs stopPropagation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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