简体   繁体   中英

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

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/

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM