Webkit [Chrome / Safari] workaround javascript select focus error (when using tabs between fields)

a similar problem appeared here: 3380458 :

When trying to select focus using the following jquery it does not work in webkit:

$('#out').focus(function(){
  $('#out').select();
});

Effectively Webkit [Chrome / Safafi] does not highlight all the text in the Focus field. This is a known bug with a workaround as shown below. THIS preset workaround using jquery works when focus happens with the mouse:

$('#out').focus(function () {
    $('#out').select().mouseup(function (e) {
        e.preventDefault();
        $(this).unbind("mouseup");
    });
});

Problem: This workaround does not work when the field is focused by pressing the tab key (when the field before it is in focus). The cursor appears at the beginning of the field and no text is selected. I have tried several things but cannot handle this workaround.

Thanks a lot - James

+1
4

, , - /CSS/javascript. - Chrome . , , , webkit, SECOND:

http://cornsyrup.org/~larry/select.html

, , setTimeout :

    $(document).ready(function() {
        $('.texty').focus(texty_focus);
    });
    function texty_focus() {
        var input = $(this);
        setTimeout(function() { 
            input.select();
        },10);
    }

( , iPad ), , , .

+3

, jQuery:

onclick="var self = this;setTimeout(function() {self.select();}, 0);"
+4

, , :

// CODE FROM GWT
TexBox.addFocusHandler(new FocusHandler()
{
  public void onFocus(FocusEvent event) {
    Timer timer = new Timer() { 
      public void run() { 
        TexBox.setSelectionRange(0, 0);
      }
    }; 
    timer.schedule(10); // WAIT 1 MILLISECOND :)
  }
}

while writing an event handler, just wait a minute before setting the selection to 0;

+2
source

Another variant:

var selectOnFocus = function (element) {
    if ($.browser.webkit) {
        element.click(function () {
            if ($(this).is(":focus")) {
                this.select();
            }
        });
    } else {
        element.focus(function () {
            this.select();
        });
    }
};
0
source

All Articles