How to Disable Enter Key in Textarea in Javascript
In this tutorial, you will learn how to disable enter key in textarea in javascript. In a standard keyboard layout, the enter key is located just above the shift key. Depending upon the operation, it can be used alone or with a combination of other keys.
To create an input field in the HTML, we make use of the input element and textarea element. The textarea element is used whenever you are expecting a multiline input field. As soon as you press enter key while typing in the textarea element, the cursor pointer will be moved to the beginning of the next line.
There are multiple ways to avoid such behavior, but we will go with the simplest approach. Whenever we press a key on the keyboard, certain keyboard events are triggered. The event object contains methods and properties which provide us the complete details about the keyboard event.
To keep things simple, we are going to monitor keydown
event and in the event handler function, we will verify whether an enter key is pressed or not using the key
property of the event object. To avoid the default behavior of the enter key, we will use preventDefault()
method.
In the following example, we have an input field. As soon as the user presses an enter key while typing in the input field, we will prevent it from moving to the next line. Please have a look over the code example and the steps given below.
HTML & CSS
- We have 3 elements in the HTML file (
div
,h1
, andtextarea
). - The
div
element is just a wrapper for the rest of the elements. We are usingstyle
attribute with div element to center align the child elements. - The
innerText
for theh1
element is“Result”
. - We have also included our javascript file
script.js
with ascript
tag at the bottom.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <div style="text-align: center"> <textarea cols="50" rows="5" placeholder="Enter Text"></textarea> <h1>Result</h1> </div> <script src="script.js"></script> </body> </html>
Javascript
- We have selected the
textarea
element andh1
element usingdocument.querySelector()
method and stored them in theinput
andresult
variables respectively. - We have attached
keydown
event listener to thetextarea
element. - In the event handler function, we are using
if
statement andkey
property of the event object to verify whether enter key is pressed or not. - If yes, we will show
“Enter Key Disabled”
in theh1
element using theinnerText
property and also callpreventDefault()
method to prevent the default behavior. - If no, we will show
“Result”
in theh1
element which is the defaultinnerText
.
let input = document.querySelector("textarea"); let result = document.querySelector("h1"); input.addEventListener("keydown", (e) => { if (e.key == "Enter") { result.innerText = "Enter Key Disabled"; e.preventDefault(); } else { result.innerText = "Result"; } });