
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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 −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initialscale=1.0"> <title>Document</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> </head> <body> <input type="text" id="name"/> <script> document.getElementById("name").addEventListener("keydown", function(event) { if (!event) { var event = window.event; } event.preventDefault(); if (event.keyCode == 13){ login(); } }, false); function login(){ console.log("submitted successfully...."); } </script> </body> </html>
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.
Output
This will produce the following output −
When you press ENTER key, the following message would be visible on console as in the below screenshot −
- Related Articles
- What is the jQuery Event for user pressing enter in a textbox?
- How to detect pressing Enter on keyboard using jQuery?
- Lose input hints (get focus) after pressing mouse cursor in a textbox with JavaScript
- How to fire after pressing ENTER in text input with HTML?
- How to simulate pressing enter in html text input with Selenium?
- How to trigger the HTML button after hitting enter button in the textbox using JavaScript?
- JavaScript Trigger a button on ENTER key
- Call the same function when clicking a Button and pressing Enter in Tkinter
- How to call a JavaScript function on submit form?
- Change value of input on form submit in JavaScript?
- How to stop refreshing the page on submit in JavaScript?
- How to trigger a button click on keyboard "enter" with JavaScript?
- JavaScript input type=submit is not working?
- Enter values with prompt and evaluate on the basis of conditions in JavaScript?
- Enter key press event in JavaScript?

Advertisements