-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpreventDefault.html
More file actions
39 lines (36 loc) · 1.09 KB
/
preventDefault.html
File metadata and controls
39 lines (36 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Events: preventDefault</title>
<style>
body { border: 2px solid red; padding: 20px;}
form { border: 2px solid green;}
button { border: 2px solid blue;}
</style>
</head>
<body>
<form method="POST" action="">
<input type="submit" value="click me!" />
</form>
<a class="toggle" href="#">toggle preventDefault();</a>
<script>
(function(window) {
// Handler that calls preventDefault on the event.
var handler = function(event) {
if (toggler) {
event.preventDefault();
}
},
form = window.document.body.querySelector('form'),
toggler = true;
// The handler will effectively prevent the native submit event from happening.
form.addEventListener('submit', handler);
// Little toggler, using scoped toggler variable.
window.document.body.querySelector('.toggle').addEventListener('click', function(e) {
toggler = !toggler;
})
})(this);
</script>
</body>
</html>