0

trying to figure out why this is happening - I have an input text field and I want all the text to be highlighted when the field receives focus. This happens, very quickly, and then all of the text is unselected. Any idea why this would occur? Here's the code I'm using:

$("#permalink").focus(function(){
    this.select();
});
Manuel van Rijn
  • 10,170
  • 1
  • 29
  • 52
mheavers
  • 29,530
  • 58
  • 194
  • 315

3 Answers3

5

You need to override the mouseup event on the input element (as mentioned in this post - thanks MrSlayer!)

See here for example: http://jsfiddle.net/f8TdX/

Community
  • 1
  • 1
joshschreuder
  • 1,413
  • 2
  • 18
  • 32
1

This is an issue in WebKit. The best option is to use a combination of the focus and mouseup events. The following comes from another answer to a similar question.

$("#permalink").focus(function() {
    var $this = $(this);

    $this.select();

    window.setTimeout(function() {
        $this.select();
    }, 1);

    // Work around WebKit's little problem
    $this.mouseup(function() {
        // Prevent further mouseup intervention
        $this.unbind("mouseup");
        return false;
    });
});
Community
  • 1
  • 1
Tim Down
  • 318,141
  • 75
  • 454
  • 536
0

Give this a shot

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

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

Community
  • 1
  • 1
Nick Benedict
  • 547
  • 1
  • 3
  • 12