Alex Rivera | Logout

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

Asked 2009-01-26T18:01:08.597
360

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

Edit
Report

3 Answers

397
$(document).ready(function() {
    $("input:text").focus(function() { $(this).select(); } );
});
answered 2009-01-26T18:06:06.123
11

my solution is to use a timeout. Seems to work ok

$('input[type=text]').focus(function() {
    var _this = this;
    setTimeout(function() {
        _this.select();
    }, 10);
});
answered 2013-01-29T00:09:30.590
3

This will work, Try this -

<input id="textField1" onfocus="this.select()" onmouseup="return false" /> 

Works in Safari/IE 9 and Chrome, I did not get a chance to test in Firefox though.

answered 2013-02-06T06:34:14.010

Your Answer