Selecting text on focus using jQuery not working in Safari and Chrome

前端 未结 9 1746
耶瑟儿~
耶瑟儿~ 2020-11-28 20:45

I have the following jQuery code (similar to this question) that works in Firefox and IE, but fails (no errors, just doesn\'t work) in Chrome and Safari. Any ideas for a wo

相关标签:
9条回答
  • 2020-11-28 21:23

    This should work also in chrome:

    $("#souper_fancy").focus(function() {
        var tempSouper = $(this);
        setTimeout(function(){
            tempSouper.select();
        },100);
    });
    
    0 讨论(0)
  • 2020-11-28 21:26

    This works fine for input type="text" elements. What kind of element is #souper_fancy?

    $("#souper_fancy").focus(function() {
        $(this).select();
    });
    
    0 讨论(0)
  • 2020-11-28 21:27
    $('#randomfield').focus(function(event) {
        setTimeout(function() {$('#randomfield').select();}, 0);
    });
    
    0 讨论(0)
  • 2020-11-28 21:27

    Just preventing default on mouseup causes the text selection to be ON at all times. The MOUSEUP event is responsible to clear the text selection. However, by preventing its default behaviour, you are unable to deselect the textusing the mouse.

    To avoid that and get the text selection to work again, you can set a flag on FOCUS, read it from MOUSEUP and reset it so future MOUSEUP events will work as expected.

    $("#souper_fancy").focus(function() {
        $(this).select();
    
        //set flag for preventing MOUSEUP event....
        $this.data("preventMouseUp", true);
    });
    
    $("#souper_fancy").mouseup(function(e) {
        var preventEvent = $this.data("preventMouseUp");
    
        //only prevent default if the flag is TRUE
        if (preventEvent) {
            e.preventDefault();
        }
    
        //reset flag so MOUSEUP event deselect the text
        $this.data("preventMouseUp", false);
    });
    
    0 讨论(0)
  • 2020-11-28 21:27

    If anyone comes again across this problem I got here a pure JS solution which is (at the moment) working on all browsers incl. mobile

    <input type="text" value="Hello world..." onFocus="window.setTimeout(() => this.select());">
    

    (without setTimeout() it's not working on Safari, mobile Safari and MS Edge)

    0 讨论(0)
  • 2020-11-28 21:29

    Use setSelectionRange() inside of a callback to requestAnimationFrame():

    $(document).on('focus', '._selectTextOnFocus', (e) => {
        var input = e.currentTarget;
        var initialType = e.currentTarget.type;
    
        requestAnimationFrame(() => {
            // input.select() is not supported on iOS
            // If setSelectionRange is use on a number input in Chrome it throws an exception,
            // so here we switch to type text first.
            input.type = "text";
            input.setSelectionRange(0, Number.MAX_SAFE_INTEGER || 9999);
            input.type = initialType;
        });
    });
    

    Use setSelectionRange() instead of select() since select() doesn't work in mobile Safari (see Programmatically selecting text in an input field on iOS devices (mobile Safari)).

    It is necessary to wait using requestAnimationFrame before selecting the text, otherwise the element isn't correctly scrolled into view after the keyboard comes up on iOS.

    When using setSelectionRange() it is important to set the input type to text, otherwise it may throw exceptions on Chrome (see selectionStart/selectionEnd on input type="number" no longer allowed in Chrome).

    0 讨论(0)
提交回复
热议问题