Problem with Chrome form handling: input onfocus=“this.select()?

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

Up vote 4 down vote favorite 1 share g+ share fb share tw.

I'm using the following HTML code to autoselect some text in a form field when a user clicks on the field: 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?

Html forms google-chrome webkit link|improve this question edited Aug 28 '11 at 0:00Matthew Crumley17k52850 asked May 30 '10 at 14:59binaryorganic318311 71% accept rate.

That will handle the mouse, but not the keyboard. It would be much better to keep onfocus. I suspect the problem is something to do with the call to .select() rather than which event is being fired.

– Dan M Jun 3 '10 at 20:35 2 @Dan, but tabbing into fields using the keyboard automatically selects their contents anyway. – Lee Kowalkowski Jun 4 '10 at 9:44 @Lee, I can't think of any that don't do that, but that behavior is dependent on the browser implementing it. – Tyler Crompton Aug 28 '11 at 0:09 @Tyler, yes, but if a browser didn't behave that way, it wouldn't be a great idea to try to make it behave differently.

That would be alien to users familiar with that particular browser. It's not just the browser that selects text when tabbing into fields, most native O/S form fields do it too. – Lee Kowalkowski Aug 31 '11 at 7:47 @Lee, but the OP asked for a behavior that isn't typical anyway.

– Tyler Crompton Sep 1 '11 at 4:43.

If you really insist on sticking with onfocus, then you'll need to add onmouseup="return false" too.

The way I got around this was by creating a wrapper function that uses setTimeout() to delay the actual call to select(). Then I just call that function in the focus event of the textbox. Using setTimeout defers the execution until the call stack is empty again, which would be when the browser has finished processing all the events that happened when you clicked (mousedown, mouseup, click, focus, etc).

It's a bit of a hack, but it works. Function selectTextboxContent(textbox) { setTimeout(function() { textbox.select(); }, 10); } Then you can do something like this to do the selection on focus.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions