I'm working with some auto-completion code. setSelectionRange()
is used to select text been completed in oninput
event handler. It works at least in Firefox 14, but not in Chrome(6, 17).
Simplified code snippet demonstrating the problem is like this:
<input type='text' oninput='select()' />
function select(e){
var s = this.value;
if (s.length)
this.setSelectionRange(s.length-1, s.length);
}
I debugged the code in chrome, and it turns out that text has been selected at first right after the setSelectionRange()
been executed, but the selection disappeared later.
If i bind the handler to onclick
instead of oninput
, like this:
<input type='text' onclick='select()' />
then both browsers work fine.
Can anyone please give me some clue to make selection work in Chrome?
this.focus()
before the call to setSelectionRange(). See example here developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/… – Lagrange