Disabling arrow key in text area using JavaScript.

JavascriptWeb DevelopmentObject Oriented Programming

Following is the code for disabling arrow key in text area in JavaScript −


 Live Demo

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
   body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
   .result {
      font-weight: 500;
      font-size: 18px;
      color: blueviolet;
<h1>Disabling arrow keys in a text area</h1>
<textarea class="AreaText">
Hello world this is some sample text inside the text area element
<div class="result"></div>
<button class="Btn" style="margin: 15px;">Disable arrows</button>
<h3>Click on the above button to disable scrolling using arrows in the above textArea</h3>
   let BtnEle = document.querySelector(".Btn");
   let resEle = document.querySelector(".result");
   BtnEle.addEventListener("click", () => {
         (event) => {
            if ([32, 37, 38, 39, 40].indexOf(event.keyCode) > -1) {
      resEle.innerHTML = "Scrolling using arrow keys is disabled";


On clicking the Disable arrows the scrolling wont work with arrows −

Published on 16-Jul-2020 17:04:54