Javascript Form How to - Handle text selected/highlighted event for TextArea








Question

We would like to know how to handle text selected/highlighted event for TextArea.

Answer


<!--from   ww w  . j av  a  2 s  .  c  o m-->
<html>
<head>
    <script language="JavaScript">
    function setText(){
      document.myForm.myTextArea2.value = document.myForm.myTextArea1.defaultValue;
    }
    </script>
</head>
<body>
    <form name="myForm">
       <textarea name="myTextArea1" rows=2 cols=50 onSelect='setText()'>
       Here is some text in my text area.
       </textarea>
       <br>
       <textarea name="myTextArea2" rows=2 cols=50>
       </textarea>
    </form>
</body>
</html>

The code above is rendered as follows: