Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript select text in textarea onload

How do I automatically select the text that is in a textarea when the page loads using JavaScript?

like image 744
Web_Designer Avatar asked Apr 15 '11 16:04

Web_Designer


2 Answers

JSFiddle Demo

You can do it this way:

HTML:

<textarea id='mytext'>Testing 1 2 3</textarea>

JavaScript:

 window.onload = document.getElementById('mytext').select();

Where mytext is your textarea

like image 158
KJYe.Name Avatar answered Sep 23 '22 11:09

KJYe.Name


Try this:

Textarea:
    <textarea rows="3" id="txtarea"  style="width:200px" >This text you can select all by clicking here </textarea>

    <script type="text/javascript">

        document.getElementById('txtarea').focus();
        document.getElementById('txtarea').select();

    </script>
like image 37
Anish Avatar answered Sep 23 '22 11:09

Anish