How to disable the Enter key on the keyboard using JavaScript

Home > Search > How-to
  by

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>

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments