Skip to content

Instantly share code, notes, and snippets.

@mihailik
Last active December 2, 2015 10:10
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 mihailik/69e762837a4dc8276a2e to your computer and use it in GitHub Desktop.
Save mihailik/69e762837a4dc8276a2e to your computer and use it in GitHub Desktop.
disabling keyboard editing via keydown.preventDefault()
<html>
<head><title>disabling keyboard editing via keydown.preventDefault()</title></head>
<body>
<h2>disabling keyboard editing via keydown.preventDefault()</h2>
<textarea id=txt></textarea>
<script>
window.onload = function() {
var txt = document.getElementById('txt');
function handleCancelEvent(e) {
if (e.preventDefault) e.preventDefault();
if (e.stopPropagation) e.stopPropagation();
if (txt.value && txt.value != ',') {
if (txt.value != ';')
document.title = '['+txt.value+'] ' + (txt.value||'').length;
txt.value = ',';
}
else {
txt.value = ';';
}
txt.selectionStart = txt.selectionEnd = 0;
txt.selectionStart = 1;
return false;
}
txt.onkeydown = txt.onkeyup = txt.onkeypress = handleCancelEvent;
if (txt.addEventListener) txt.addEventListener('compositionstart', handleCancelEvent, true);
if (txt.addEventListener) txt.addEventListener('compositionupdate', handleCancelEvent, true);
txt.focus();
};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment