JavaScript Submit textbox on pressing ENTER?

The ENTER has the key code 13. We will use the event.keycode to check whether ENTER is pressed or not. Following is the code −


 Live Demo

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initialscale=1.0">
<link rel="stylesheet" href="//">
<script src=""></script>
<script src=""></script>
<input type="text" id="name"/>
   function(event) {
      if (!event) {
         var event = window.event;
      if (event.keyCode == 13){
   }, false);
   function login(){
      console.log("submitted successfully....");

To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option “Open with Live Server” in VS Code editor.


This will produce the following output −

When you press ENTER key, the following message would be visible on console as in the below screenshot −