Coder Perfect

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


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() {

Answered by Tomas Kirda

Solution #4

$(document).ready(function() {
        .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();">Text is here</textarea>

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

Answered by aaa

Post is based on