FOLLOW ME ON TWITTER >

About
Tom Hubbard-Green is the E-marketing and Social Media Manager at Alzheimer's Society and a freelance technology journalist.
His articles have been published in a variety of magazines and industry publications including Figaro Digital, Org Zine, O2 Venue magazine and The City magazine.
The views expressed on this blog are his own. Obviously.



Twitter Stream
Categories
Blogroll
- Alzheimer's Society
- Find50-Marketing
- Lifehacker
- The Black Sparrows band
- Touch Arcade
- Wired UK
- XKCD Comic
Archive
- April 2012
- February 2012
- May 2011
- February 2011
- November 2010
- September 2010
- June 2010
- February 2010
- January 2010
- October 2009
- September 2009
- August 2009
- July 2009
Tutorial: how to create text boxes with the content automatically selected
If you need your audience to copy a specific bit of text or a URL you’re going to want to make it as simple for them as possible. A great way of doing this is to create a text box that automatically highlights the contents when a user clicks on it. This is easy to achieve with a just a few lines of JavaScript.
First of all you want to create your text box. This can be easily styled with CSS for this example we’ll just use a plain box with a size of 2 rows and 40 columns. The code you’ll need is:
<textarea rows="2" cols="40" name="share">This text will be automatically highlighted when you click on the box
</textarea>
This creates the text area and the content but it isn’t automatically selected when you click in the box. To do that you just need to add a few bits of JavaScript:
<textarea rows="2" cols="40" name="share" onclick="this.focus();this.select()">This text will be automatically highlighted when you click on the box
</textarea>
By adding onclick="this.focus();this.select()" the content inside the text area will now be automatically highlighted when you click in the box.