How to disable default behavior of when you press enter with JavaScript?

For this, you need to use keyDown as well as preventDefault(). Following is the JavaScript code −


 Live Demo

<!DOCTYPE html>
<html lang="en">
<link rel="stylesheet" href="//">
<script src=""></script>
<script src=""></script>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<input type="time" id="disableDefaultTime">
   const pressEnter = (event) => {
      if (event.key === "Enter") {

To run the above program , just save the file name anyName.html(index.html) and right click on the file and select the option open with live server in VSCode editor.


Following is the output. When you press Enter key nothing will be displayed.

You need to use keyDown to get time as in the below screenshot −

Updated on: 14-Jul-2020


Kickstart Your Career

Get certified by completing the course

Get Started