Coder Perfect

When a textbox obtains focus, it will select all of its contents (Vanilla JS or jQuery)

Problem

When a textbox gains attention, what is a Vanilla JS or jQuery solution that selects all of the contents of the textbox?

Asked by Jon Erickson

Solution #1

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

Answered by John Sheehan

Solution #2

Answered by Zach

Solution #3

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

Answered by Tomas Kirda

Solution #4

$(document).ready(function() {
    $("input:text")
        .focus(function () { $(this).select(); } )
        .mouseup(function (e) {e.preventDefault(); });
});

Answered by Yogesh Agrawal

Solution #5

jQuery is not the same as JavaScript, which is more user-friendly in some circumstances.

Consider the following scenario:

<textarea rows="10" cols="50" onclick="this.focus();this.select()">Text is here</textarea>

MDN/CSS Tricks/MDN/CSS Tricks/CSS Tricks/CSS

Answered by aaa

Post is based on https://stackoverflow.com/questions/480735/select-all-contents-of-textbox-when-it-receives-focus-vanilla-js-or-jquery