Looking for a better workaround to Chrome select on focus bug
The accepted answer (and basically every other solution I found so far) does not work with keyboard focus, i. e. pressing tab, at least not in my Chromium 21. I use the following snippet instead:
$('#out').focus(function () {
$(this).select().one('mouseup', function (e) {
$(this).off('keyup');
e.preventDefault();
}).one('keyup', function () {
$(this).select().off('mouseup');
});
});
e.preventDefault()
in the keyup
or focus
handler does not help, so the unselecting after a keyboard focus seems to not happen in their default handlers, but rather somewhere between the focus
and keyup
events.
As suggested by @BarelyFitz, it might be better to work with namespaced events in order to not accidentally unbind other event handlers. Replace 'keyup'
with 'keyup.selectText'
and 'mouseup'
with 'mouseup.selectText'
for that.
How about this?
$('#out').focus(function () {
$('#out').select().mouseup(function (e) {
e.preventDefault();
$(this).unbind("mouseup");
});
});