Javascript Form How to - Handle key down event for TextArea








Question

We would like to know how to handle key down event for TextArea.

Answer


<!--from  ww w.  j a  v  a  2s.  co  m-->
<html>
    <head>
    <script>
    function showDialog(){
      console.log("A key was pressed down");
    }
    </script>
</head>
<body>
    <form name="myForm">
      <textarea name="myTextArea" rows=2 cols=50 onKeyDown='showDialog()'>
      Here is some text in my text area.
      </textarea>
    </form>
</body>
</html>

The code above is rendered as follows: