Search code examples
htmlformsgoogle-chromewebkit

problem with Chrome form handling: input onfocus="this.select()"


I'm using the following HTML code to autoselect some text in a form field when a user clicks on the field:

<input onfocus="this.select()" type="text" value="Search">

This works fine in Firefox and Internet Explorer (the purpose being to use the default text to describe the field to the user, but highlight it so that on click they can just start typing), but I'm having trouble getting it to work in Chrome. When I click the form field in Chrome the text is highlighted for just a split second and then the cursor jumps to the end of the default text and the highlighting goes away.

Any ideas on how to get this working in Chrome as well?


Solution

  • Instead of binding to onfocus event you must bind this action into onclick event and it will work as you wanted.

    <input onclick="this.select()" id="txt1" name="txt1" type="text" value="Search">