I'm displaying a textarea
input via PHP using the command :
print " '<textarea rows='16' cols='30'>$flist'</textarea><BR>";
I would like the textarea
to have focus and contents $flist
be selected automatically. All I've found to date is how to select when the input is clicked.
How would I accomplish this using javascript (no jquery)?
Thanks, Walt
Write your code as below:-
// Assign a id to textarea and rewrite below line :-
print "<textarea rows='16' cols='30'>$flist</textarea><BR>";
In Js use getElementById:-
<script>
function setFocusToTextBox(){
document.getElementById("mytext").focus();
}
</script>
Try this:
var textarea = document.getElementsByTagName("textarea")[0];
textarea.focus();
textarea.select();
if you have more then one textarea on a page you will need to add id to textarea:
print "<textarea id='text' rows='16' cols='30'>$flist</textarea><BR>";
and use getElementById:
var textarea = document.getElementById("text");
You could use focus()
and select()
:
document.querySelector('textarea').focus();
document.querySelector('textarea').select();
Your PHP code should be :
print "<textarea rows='16' cols='30'>$flist</textarea><BR>";
Hope this helps.
document.querySelector('textarea').focus();
document.querySelector('textarea').select();
<textarea id='text' rows='16' cols='30'>text here</textarea><BR>
</div>
So here's what actually worked...
print " <textarea id='txarea' rows='16' cols='30'>$flist</textarea><BR>";
print " <script>
";
print " document.getElementById('txarea').focus();
";
print " document.getElementById('txarea').select();
";
print " </script>
";
It ended up being a combination of the responses. Thanks to all for the clues...