2011-04-15 20 views
5

¿Cómo selecciono automáticamente el texto que está en un área de texto cuando la página se carga con JavaScript?javascript seleccionar texto en textarea onload

+1

posible duplicado de [Javascript para preseleccionar área de texto al cargar la página] (http://stackoverflow.com/questions/3646004/javascript-to-pre-select-textarea-on-page-load) – Neal

+0

@ Neal gracias por el enlace. No vi esa pregunta. –

+0

El enlace de Neal está basado en jQuery – kjy112

Respuesta

8

JSFiddle Demo

Usted puede hacerlo de esta manera:

HTML:

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

JavaScript:

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

Dónde mitexto es su área de texto

-1

esta es la respuesta publicada en el dupe:

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

tienes que decir: jQuery! – raiserle

1

Prueba esto:

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

En la función onload de su cuerpo, realizar una llamada a la función de selección de su área de texto ...

HTML :

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

JS:

var highlightTextArea = function(){ 
    document.getElementById('myTextArea').select(); 
} 
Cuestiones relacionadas