How to prevent text from auto-selecting when focusing an HTML input element

Quite frequently I start typing text into an HTML <input type='text'> element, then Alt-Tab to a different window to look something up, then Alt-Tab back to continue typing. Firefox automatically selects all of the text when the input field is refocused, which causes me to overwrite it because I automatically start typing  with the expectation that it will continue where I left off. Here is a partial mitigation for that:

<input type='text' onfocus='setTimeout(function () { input.selectionStart = input.selectionEnd; }, 1);'>

I say “partial” because it doesn’t remember the cursor position, instead it moves the cursor to the end of the input field. Usually this is what you want, but not always.


Add new comment