JavaScript Tip – Select All Text by Clicking on Text Field or Textarea Box

This script highlight all text in textarea or text field (input tag) by clicking on it.  It allows users to reduce their manual work.  For instance when users need to copy to the clipboard all text quickly or quickly delete the text from the field.  For example: to copy a piece of code or to delete a username and password on login web page.

Code

The script is really very simple. Text field must have unique identifier, this indentifier will be passed to the SelectAll() function.
The function call only two methods: focus() and select().

<script type="text/javascript">
function SelectAll(id)
{
    document.getElementById(id).focus();
    document.getElementById(id).select();
}
</script>

Textarea:<br>
<textarea rows="3" id="txtarea" onClick="SelectAll('txtarea');" style="width:300px" >You can select all by clicking here</textarea>

Input TextBox:<br>
<input type="text" id="txtfield" onClick="SelectAll('txtfield');" style="width:300px" value = "You can select all by clicking here" />
Advertisements
Published in: on September 30, 2009 at 6:42 pm  Comments (2)  

The URI to TrackBack this entry is: https://blackbeltreview.wordpress.com/2009/09/30/select-all-text-by-clicking-on-text-field-or-textarea-box/trackback/

RSS feed for comments on this post.

2 CommentsLeave a comment

  1. Awesome blog!

    I thought about starting my own blog too but I’m just too lazy so, I guess Ill just have to keep checking yours out.
    LOL,


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: