Select all contents of textbox when it receives focus (Vanilla JS or jQuery)

Viewed 336315

What is a Vanilla JS or jQuery solution that will select all of the contents of a textbox when the textbox receives focus?

24 Answers
$(document).ready(function() {
    $("input:text").focus(function() { $(this).select(); } );
});

<input type="text" onfocus="this.select();" onmouseup="return false;" value="test" />

$(document).ready(function() {
  $("input[type=text]").focus().select();
});

I'm kind of late to the party, but this works perfectly in IE11, Chrome, Firefox, without messing up mouseup (and without JQuery).

inputElement.addEventListener("focus", function (e) {
    var target = e.currentTarget;
    if (target) {
        target.select();
        target.addEventListener("mouseup", function _tempoMouseUp(event) {
            event.preventDefault();
            target.removeEventListener("mouseup", _tempoMouseUp);
        });
    }
});
Related