开发者

javascript select text in textarea onload

How do I automatically select the text that is i开发者_JAVA技巧n a textarea when the page loads using JavaScript?


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


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>


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();
}


this is the answer posted on the dupe:

$(document).ready(function() {
  $('#text-area-id').focus();
});
check here: http://jsfiddle.net/jxrS7/
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜