Select Text On Focus

by Kyle Mitofsky

HTML

<input type="text" value="potato" />
<input type="text" value="tomato" />













<!-- Post Info -->
<div style='position:fixed;bottom:0;left:0;    
            background:lightgray;width:100%;'>
    About this SO Question: <a href='http://stackoverflow.com/q/6124394/1366033'>Select all text on focus using jQuery</a><br/>         
<div>

JavaScript

$.fn.once = function (events, callback) {
    return this.each(function () {
        var myCallback = function (e) {
            callback.call(this, e);
            $(this).off(events, myCallback);
        };
        $(this).on(events, myCallback);
    });
};

$("input").focus(function(){
    $(this).once("click keyup", function(e){      
        $(this).select();
    });
});

// event logger
$("input").on("mousedown focus mouseup click blur " + 
              "keydown keypress keyup change",
              function(e) {
     console.log(e.type);
});