mheavers
mheavers

Reputation: 30188

jquery - field selects all text then unselects it on focus

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();
});

Upvotes: 0

Views: 861

Answers (3)

Nick Benedict
Nick Benedict

Reputation: 557

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)

Upvotes: 0

Tim Down
Tim Down

Reputation: 324687

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;
    });
});

Upvotes: 1

joshschreuder
joshschreuder

Reputation: 1463

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/

Upvotes: 5

Related Questions