Reputation: 20965
How can I highlight all the text within a textbox when it is clicked using jQuery?
Upvotes: 2
Views: 625
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
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
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