JavaScript - Disable the Enter key on the keyboard

by
Jeremy Canfield |
Updated: March 20 2020
| JavaScript articles
The following code will disable the Enter key on the user's keyboard. One example where this is practical is when data entered into an input field, such as a search engine, is programmed to disable data as the user types. The source of this code comes from http://webcheatsheet.com/javascript/disable_enter_key.php.
<script type="text/javascript">
function DisableEnterKey(evt) {
var evt = (evt) ? evt : ((event) ? event : null);
var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
if ((evt.keyCode == 13) && (node.type==\"text\")) {return false;}
}
document.onkeypress = DisableEnterKey;
</script>
Did you find this article helpful?
If so, consider buying me a coffee over at