How to disable arrow key in textarea using JavaScript ?

Given an HTML element containing the <textarea> element and the task is to disable scrolling through arrow keys with the help of JavaScript.

Approach 1:

  • Add an event listener onkeydown on the window.
  • If the event happens then check if the keys are arrow or not.
  • If arrow key is pressed then prevent its default behaviour.

Example: This example implements the above approach.



filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML> 
<html
  
<head
    <title
        How to disable arrow key in
        textarea using JavaScript ?
    </title>
      
    <style>
        #t {
            height: 100px;
            width: 300px;
            background: green;
            color: white;
        }
    </style>
</head
  
<body style = "text-align:center;"
      
    <h1 style = "color:green;"
        GeeksforGeeks 
    </h1>
      
    <p id = "GFG_UP" style =
        "font-size: 15px; font-weight: bold;">
    </p>
      
    <textarea id = "t">
        JavaScript was once upon a time used
        only in client side(browser), but node
        js (execution engine/run time/web 
        server) have made possible to run
        javascript on server side. JavaScript
        has stormed the web technology and
        nowadays small software ventures to
        fortune 500, all are using node js
        for web apps. 
    </textarea>
    <br>
      
    <button onclick = "gfg_Run()"
        click here
    </button>
      
    <p id = "GFG_DOWN" style = "color:green;
        font-size: 20px; font-weight: bold;">
    </p>
      
    <script>
        var el_up = document.getElementById("GFG_UP");
        var el_down = document.getElementById("GFG_DOWN");
          
        el_up.innerHTML = "Click on the button to disable"
                    + " scrolling through arrow keys.";
          
        function gfg_Run() {
            window.addEventListener("keydown", function(e) {
                if([32, 37, 38, 39, 40].indexOf(e.keyCode) > -1){
                    e.preventDefault();
                }
            }, false);
              
            el_down.innerHTML = 
                "Scrolling from arrow keys is disabled.";
        }         
    </script
</body
  
</html>

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:

Approach 2:

  • This example is quite similar to the previous one. It also maintains an array of which keys are pressed and when keyup event happens it removes them from array.
  • Add a event listener onkeydown on the window.
  • If the event happens then check if the keys are arrow or not.
  • If arrow key is pressed then prevent its default behaviour.

Example: This example implements the above approach.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML> 
<html
  
<head
    <title
        How to disable arrow key in
        textarea using JavaScript ?
    </title>
      
    <style>
        #t {
            height: 100px;
            width: 300px;
            background: green;
            color: white;
        }
    </style>
</head
  
<body style = "text-align:center;"
      
    <h1 style = "color:green;"
        GeeksforGeeks 
    </h1>
      
    <p id = "GFG_UP" style =
        "font-size: 15px; font-weight: bold;">
    </p>
      
    <textarea id = "t">
        JavaScript was once upon a time used
        only in client side(browser), but node
        js (execution engine/run time/web 
        server) have made possible to run
        javascript on server side. JavaScript
        has stormed the web technology and
        nowadays small software ventures to
        fortune 500, all are using node js
        for web apps. 
    </textarea>
    <br>
      
    <button onclick = "gfg_Run()"
        click here
    </button>
      
    <p id = "GFG_DOWN" style = "color:green;
        font-size: 20px; font-weight: bold;">
    </p>
      
    <script>
        var el_up = document.getElementById("GFG_UP");
        var el_down = document.getElementById("GFG_DOWN");
          
        el_up.innerHTML = "Click on the button to disable"
                + " scrolling through arrow keys.";
          
        function gfg_Run() {
            var key = {};
            window.addEventListener("keydown", function(e) {
                key[e.keyCode] = true;
                switch(e.keyCode){
                    case 37: case 39: case 38: case 40:
                    case 32: e.preventDefault(); break; 
                    default: break;
                }
            }, false);
              
            window.addEventListener('keyup', function(e){
                key[e.keyCode] = false;
            }, false);
              
            el_down.innerHTML = 
                "Scrolling from arrow keys is disabled.";
        }         
    </script
</body
  
</html>

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.