
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JavaScript Trigger a button on ENTER key
To trigger a button on ENTER key, the code is as follows −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .sample, .result { font-size: 18px; font-weight: 500; color: red; } .result { color: blueviolet; } </style> </head> <body> <h1>Trigger a button on ENTER key</h1> Enter text<input type="text" class="sample" /> <button class="Btn">CLICK HERE</button> <div class="result"></div> <h3> Press enter in the above text field to trigger the button </h3> <script> let resultEle = document.querySelector(".result"); let sampleEle = document.querySelector(".sample"); sampleEle.addEventListener("keyup", (event) => { if (event.keyCode === 13) { event.preventDefault(); document.querySelector(".Btn").click(); } }); document.querySelector(".Btn").addEventListener("click", () => { resultEle.innerHTML ="Button has been clicked and text = " + sampleEle.value; }); </script> </body> </html>
Output
On entering some text and pressing enter key in the input field −
- Related Questions & Answers
- How to trigger a button click on keyboard "enter" with JavaScript?
- How to trigger click event on pressing enter key using jQuery?
- Trigger a button click and generate alert on form submission in JavaScript
- Enter key press event in JavaScript?
- JavaScript Submit textbox on pressing ENTER?
- Enter a number and write a function that adds the digits together on button click in JavaScript
- Detect the ENTER key in a text input field with JavaScript
- Typing Enter/Return key in Selenium.
- How to bind the Enter key to a tkinter window?
- JavaScript Sum function on the click of a button
- How do I bind the enter key to a function in Tkinter?
- How to hide a div in JavaScript on button click?
- Call the same function when clicking a Button and pressing Enter in Tkinter
- How to bind a key to a button in Tkinter?
- How can I trigger a JavaScript click event?
Advertisements