Valamas
Valamas

Reputation: 24759

setSelectionRange with on click in chrome does not select on second click

I have a function that selects the first 5 characters of a input onclick. This works for the very first click. When the input box still has focus, in chrome, a second click within the first 5 characters removes the selection and the caret is blinking at the position of the click.

I expect the first 5 characters to still-be/remain selected.

This appears to be a chrome issue as it works fine in Firefox.

Here is my fiddle: http://jsfiddle.net/valamas/3eaYq/2/

How can I get this working with chrome?

I have tried resetting the selection using

sender.setSelectionRange(0, 1);
sender.setSelectionRange(0, 5);

Upvotes: 3

Views: 3325

Answers (1)

Sean Vieira
Sean Vieira

Reputation: 160063

This is indeed an issue with Chromium (and a two year old issue at that). To make Chrome behave the same as Firefox, simply wrap the contents of doSelect in a setTimeout call:

function doSelect(sender, e) {
    setTimeout(function(){
        sender.setSelectionRange(0, 5);
    }, 0);
}

See also: the updated JS Fiddle


Update: The bug is now marked as FIXED and will be delivered in Chrome 39.

Upvotes: 7

Related Questions