This is based on Danilo Valente's answer above. I found that in Chrome I had to use a slight delay before calling this.selectionEnd = this.selectionStart
, otherwise the input text will remain selected:
This works for me in Chrome, Firefox, Safari, and IE (IE 10 tested)
$("input").focus(function() { var input = this; setTimeout(function() { input.selectionStart = input.selectionEnd; }, 1); });
Here is a demo: http://jsfiddle.net/rangfu/300ah0ax/2/
source share