onclick select all text in Text Field or Textarea Box – JavaScript

Lets suppose you have text inside a div tag or input text field and you wish to select all the content on clicking inside it, then here is what you should do:

Textarea:

<textarea onclick="SelectAll('txtarea');" id="txtarea" rows="3" name="type">
           Select all the content by clicking on it.
</textarea>

Input TextBox:

<input type="text" value="This text you can select all" onclick="SelectAll('txtfld');" id="txtfld" style="width: 200px;"/>

Here is the JavaScript function that is called above:

<script type="text/javascript">
function SelectAll(id)
{
    document.getElementById(id).focus();
    document.getElementById(id).select();
}
</script>
Tags: , ,
Subscribe to Comments RSS Feed in this post

One Response

  1. Awesome job over again! Thanks:)

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*