Javascript Form How to - Select the text in the TextArea








Question

We would like to know how to select the text in the TextArea.

Answer


       <!--   ww w . j a  v a2s  .c om-->
<html>
<head>
    <script language="JavaScript">
    function selectText(){
      document.myForm.myTextArea.select();
      document.myForm.myTextArea.focus();
    }
    </script>
</head>
<body>
    <form name="myForm">
       <textarea name="myTextArea" rows=6 cols=50>
       Here is some text in my text area.
       </textarea>
    <input type=BUTTON value="Click to Select Text" name="myButton" onClick="selectText()">
    </form>
</body>
</html>

The code above is rendered as follows: