Javascript Click to Select All Nov 27 ♦ 1 comment

Well, I was doing some work this morning on displaying the URL to certain items. This of course was to make things more user friendly for people who wanted to paste the url elsewhere within the site. Well instead of just the usual boring "displaying" the url as plain text, I figured I'd kick it up a notch and do some form/javascript stuff to make it a little bit easier for those lazy users. ;)

The Code

Well, first off, I did not write this code from scratch. This code was hard to find and edit, so therefore that is why I am posting it here so may be other people will not have trouble finding it. Again, this code allows the user to "click" on the form element and it "highlights all" of the text. This allows for easy copy and pasting!

This javascript goes at the top, preferably inside the tags. Although this javascript code can be placed pretty much anywhere in the code. It must go before the form field you are trying to use it with though. (Common sense tells us this)


function select_all(field)
{
field.focus();
field.select();
}

 

Then, for each form field that you want to "select all" when you click on it, just add this attribute to the form field:

 

onClick='select_all(this);'

Example



function select_all(field)
{
field.focus();
field.select();
}

<input type="Text" name="Test" value="I'm going to get selected!" onClick='select_all(this);'>


Closing Remarks

Thats pretty much it. If anyone has any problems with getting this to work, i'll be more than happy to help. Any questions or comments, let me know and ill address them.


 

Comments (1 So Far)
  • Greg on Apr 21 said:
  • Good

Leave a Message:

Archives

Photo Memory on Flickr!