15

I'm using some HTML5 form features to progressively enhance the user experience of my web project. One in particular is autofocus, I'm wanting to use this in a modal (lightbox) dialog that allows users to enter tags. Existing tags could be there.

The only browser I have installed that supports autofocus is Chrome 9, however when I test it there it does place the focus on the field, but also automatically selects all existing text in the field. I did not expect or want autoselect, I want autofocus.

Is my expectation wrong or is Chrome wrong?

Fer
  • 4,116
  • 16
  • 59
  • 102
  • Worth keeping an eye out for accessibility issues as described here http://webaim.org/blog/future-web-accessibility-html5-input-extensions/ – Mike Gifford Jan 24 '17 at 15:44

4 Answers4

7

Chrome decided they were wrong and changed this behaviour in Chrome 36.

I know this because I was wrongly relying on javascript .focus() and html autofocus for also selecting the text. Now it requires a .select() to select and focus in the text field. Good that we can now be specific of what we want :-)

So now Chrome behaves like e.g. Firefox.

Pascal_dher
  • 448
  • 3
  • 14
6

Read more about autofocus and html5 here: http://php.quicoto.com/autofocus-in-html5/

If you have a snippet of code we could look at, we might be able to further be of help. It looks fairly simple and painless to implement and says the feature is supported in everything but Fx and IE.

  • Well, the snippet is simply an input with the autofocus attribute included: . The example you include makes sense, yet it concerns an empty field. Like I said, the focus works, it does place the cursor in the field, yet it also selects all existing text if there is any. That is not what I want or expected. – Fer Jan 19 '11 at 21:41
  • So you are looking to add the cursor at the end of the already existing text? If that's the case, that's not autofocus and to my knowledge would likely require javascript... Regardless, autofocus is doing it's job. As Eli said, autofocus is working. Try looking into js. Sorry, wish I could be of more help. –  Jan 25 '11 at 00:37
  • Actually, I don't care whether it is placed at the beginning or the end of the text, it should focus on the field. It should not select all text in that field, as that means users overwrite any existing value as they start typing. I do not agree that autofocus should mean that existing text is automatically selected, but hey, if that really is according to spec, so be it. – Fer Feb 02 '11 at 20:45
  • As indicated in [this answer](http://stackoverflow.com/questions/4740184/html5-autofocus-autoselect/13980739#13980739), the spec *doesn't* specify whether or not the text should be selected. – shelley Jan 16 '13 at 14:50
2

From the HTML5 specification, the focusing steps do not prescribe whether or not text should be selected. User agents are neither required nor forbidden to select the text. Specifically, here is the relevant focusing step:

The user agent may apply relevant platform-specific conventions for focusing widgets.

Note: For example, some platforms select the contents of a text field when that field is focused.

shelley
  • 7,206
  • 4
  • 36
  • 63
-2

It is supposed to select the text, as input.focus() does this too.

Eli Grey
  • 35,104
  • 14
  • 75
  • 93