Categories
DHTML JavaScript Web Development

Selecting All Text in a Form Field with a Single Click

Sometimes the text in a form field is put there primarily for the purpose of being copied to the clipboard.

In these cases a little JavaScript will smooth the process, selecting the full contents of the field on the first click.

Place the following script in the section of your page:

1
2
3
4
5
<script type="text/JavaScript">
function highlight(field) {
       field.focus();
       field.select();
}</script>

Then place onClick='highlight(this);' as an attribute to any field you want treated this way:

1
<input type="text" name="myfield" size="20" value="mytext" onClick='highlight(this);'>

Leave a Reply