Skip to content

Instantly share code, notes, and snippets.

@mihailik mihailik/index.html
Last active Dec 2, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.