Ben Shelock
Ben Shelock

Reputation: 20965

Highlight Text When Clicked

How can I highlight all the text within a textbox when it is clicked using jQuery?

Upvotes: 2

Views: 625

Answers (4)

Kane Wallmann
Kane Wallmann

Reputation: 2322

This works for me in both FF and IE.

$("#user").click(function() {
  $("#user").focus();
  $("#user").select();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="text" value="username" id="user" />

Upvotes: 4

Cubic Compass
Cubic Compass

Reputation: 205

Check out the JQuery UI 'highlight' effect at http://jqueryui.com/demos/effect/

$( function() {
    $("textBoxId")
        .click( function(){
            var options = {};
            $("#textBoxId").effect("Highlight",options,500,callback);
        });
    });

Upvotes: 1

gnarf
gnarf

Reputation: 106332

$('textarea').click(function() { this.focus(); this.select(); });

That should work for all textarea's - switch the selector if need be.

I would probably go with class='autoselect' and then $('.autoselect')

Upvotes: 1

Robert Harvey
Robert Harvey

Reputation: 180788

Using jQuery to Highlight (Select) All Text in a Textbox

http://www.willstrohl.com/Blog/tabid/66/EntryId/321/Using-jQuery-to-Highlight-Select-All-Text-in-a-Textbox.aspx

Upvotes: 1

Related Questions