Select all text on focus using jQuery
http://stackoverflow.com/q/6124394/154877
by mrrodd
HTML
<div id="wrap">
<header>
<h1>select() on form field focus</h1>
</header>
<section>
<h2>Using "focus"</h2>
<input value="Some" class="old">
<input value="text" class="old">
<input value="here" class="old">
<h2>Using "mouseup"</h2>
<input value="Some" class="new">
<input value="text" class="new">
<input value="here" class="new">
</section>
<footer>This is a code example for the Stack Overflow question <br><a href="http://stackoverflow.com/q/6124394/154877">"Select all text on focus using jQuery"</a></footer>
</div>
CSS
body { color:#222; background:#fafafa; font:13px/1.4 'segoe ui', sans-serif; }
h1, h2 { margin:0 0 .5em; font:700 2.4em/1 'myriad pro', sans-serif; } h2 { color:#999; margin: 1em 0; font-size:1.6em; }
a { color:#06c; transition:.2s linear; -webkit-transition:.2s linear; } a:hover { color:#39f; }
p { margin:1em 0; } footer { color:#999; margin:3em 0 0; text-align:center; } footer a { color:#777; }
#wrap { width:500px; margin:1em auto; padding:1.5em; background:#fff; box-shadow:0 1em 2em rgba(0,0,0,.15); }
JavaScript
$("input.old").live('focus', function() { $(this).select(); });
$("input.new").live('mouseup', function() { $(this).select(); });