---
description: https://api.jquery.com/keypress/
---

# Prevent form submission on Enter key press, keyCode (ok)

![](https://2494213435-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LYjAG13rAPdbArSfRyB%2F-MQVeBThomIh3SONdX5s%2F-MQVejkigxho4_ZN0b4J%2FScreenshot_5.jpg?alt=media\&token=c020ce0a-6a75-4a95-942b-08b0be8da343)

C:\xampp\htdocs\php\test.html

```
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>keypress demo</title>
  <style>
    fieldset {
    margin-bottom: 1em;
  }
  input {
    display: block;
    margin-bottom: .25em;
  }
  #print-output {
    width: 100%;
  }
  .print-output-line {
    white-space: pre;
    padding: 5px;
    font-family: monaco, monospace;
    font-size: .7em;
  }
  </style>
  <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
</head>
<body>
  <form>
    <fieldset>
      <label for="target">Type Something:</label>
      <input id="target" type="text" value="">
    </fieldset>
  </form>
  <script type="text/javascript">
  $("#target").keypress(function(event) {
    if (event.which == 13) {
    	event.preventDefault();
      console.log(event);
    }
  });
  $('#target').on('keypress', function(event) {
  	event.preventDefault();
    switch (event.which) {
      case 13:
      console.log(13);
      break;
    }
  });
  </script>
</body>
</html>
```
