Skip to content

Instantly share code, notes, and snippets.

@iiic
Last active June 28, 2017 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iiic/8b2768a840fcfc38d42991bab323a328 to your computer and use it in GitHub Desktop.
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
<!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