Select all text in an <input /> when it gains focus

Posted by Chris Barr on Stack Overflow See other posts from Stack Overflow or by Chris Barr
Published on 2012-12-14T23:00:52Z Indexed on 2012/12/14 23:03 UTC
Read the original article Hit count: 184

Filed under:
|
|

I've got a textbox (set to readonly) and I need its' contents to be selected for easy copy/paste when it gains focus. Using the code below it only seems to quickly select the text and then unselect it for some reason.

HTML

<input id='thing' type='text' value='some text' readonly='readonly' />?

JavaScript

document.getElementById('thing').onfocus = function(){
    this.select();
};?

Fiddle: http://jsfiddle.net/cfqje/

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about html