7

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

4

4 Answers 4

10

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

1

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>
0

In the onload function of your body, place a call to the select function of your textarea...

HTML:

<body onload='highlightTextArea()'>
    <textarea id='myTextArea'>Hello World!</textarea>
</body>

JS:

var highlightTextArea = function (){
    document.getElementById('myTextArea').select();
}
-1

this is the answer posted on the dupe:

$(document).ready(function() {
  $('#text-area-id').focus();
});
check here: http://jsfiddle.net/jxrS7/
1
  • you need to say: jQuery!
    – raiserle
    Apr 17, 2014 at 0:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.