Last active
June 28, 2017 08:58
-
-
Save iiic/8b2768a840fcfc38d42991bab323a328 to your computer and use it in GitHub Desktop.
Podle input type='reset' resetuji formulář, ovšem, až po potvrzení js confirm-u
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
<!DOCTYPE HTML> | |
<meta charset="utf-8"> | |
<title>accesskey tester</title> | |
<body> | |
<p> | |
Podle input type='reset' resetuji formulář, ovšem, až po potvrzení js confirm-u | |
</p> | |
<form method="get" action="#odeslany-formular"> | |
<input type="text" value="defaultní hodnota"> | |
<input type="submit"> | |
<input type="reset" hidden> | |
</form> | |
<script> | |
(function () { | |
'use strict'; | |
const ESCAPES = ['Escape', 'Esc']; // 'Esc' in IE, 'Escape' in others | |
const ESCAPE_EVENT = 'keydown'; | |
const RESET_EVENT = 'click'; | |
const QUESTION = 'Are you sure you want to reset form?' | |
var resets = document.querySelectorAll('input[type="reset"]'); | |
for (var i = 0; i < resets.length; i++) { | |
resets[i].hidden = false; | |
resets[i].addEventListener(RESET_EVENT, function(event) { | |
event.preventDefault(); | |
if (confirm(QUESTION)) { | |
event.target.form.reset(); | |
} | |
return true; | |
}); | |
resets[i].form.addEventListener(ESCAPE_EVENT, function(event) { | |
if (ESCAPES.indexOf(event.key) !== -1) { | |
event.preventDefault(); | |
if (confirm(QUESTION)) { // using dispatchEvent() not possible because IE | |
event.target.form.reset(); | |
} | |
} | |
return true; | |
}); | |
} | |
}()); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment