I\'m using the following HTML code to autoselect some text in a form field when a user clicks on the field:
This is a solution working in Firefox, Chrome and IE, both with keyboard focus and mouse focus. It also handles correctly clicks following the focus (it moves the caret and doesn't reselect the text):
With keyboard focus, only onfocus
triggers which selects the text because this.clicked
is not set. With mouse focus, onmousedown
triggers, then onfocus
and then onclick
which selects the text in onclick
but not in onfocus
(Chrome requires this).
Mouse clicks when the field is already focused don't trigger onfocus
which results in not selecting anything.