0

Right now I use:

<body onLoad="document.getElementById('myinput').focus();">

but it doesn't seem to work on iPhone.

Ryan
  • 5,883
  • 13
  • 56
  • 93
  • That should work, provided "myinput" is the `id` of a focussable element on the page. I can't see why Mobile Safari would have an issue with it. And yet, it does: [This example](http://jsbin.com/eyekaq) works elsewhere, but not on the iPhone. [Various](http://jsbin.com/eyekaq/2) [other](http://jsbin.com/eyekaq/3) [attempts](http://jsbin.com/eyekaq/5) suggest it's the `focus` call rather than when it occurs. Odd. – T.J. Crowder Dec 02 '11 at 11:25
  • This question has been [asked before](http://stackoverflow.com/questions/979309/how-do-i-focus-an-html-text-field-on-an-iphone-causing-the-keyboard-to-come-up), more than two years ago, without a satisfactory answer. It's clearly *not* a general WebKit bug as the accepted answer there suggests, as it works fine in Chrome and I can't imagine non-mobile (deskbound? ;-) Safari has this issue either. – T.J. Crowder Dec 02 '11 at 11:27

2 Answers2

3

Autofocus will never work on iPhone. Apple decided it is not in the interest of the user, so that is that.

The best way is to add autofocus="" on the input in question. If you want to support older browsers to it is best to put the JS you've given at the end of the page instead of putting it in onload. If the page contains some images that take some time to load the onload will not fire directly and the user could already have selected another input and started typing, resulting in the text appearing in the wrong textbox once the onload fires, frustrating users.

So just put <script>document.getElementById('myinput').focus();</script> just before </body>

Gerben
  • 16,747
  • 6
  • 37
  • 56
  • 1
    Indeed. A lot of sites that have a login use autofocus. If iPhone would respect this, the user would have to dismiss the form/keyboard, and zoom out on every page he views. Highly annoying. – Gerben Dec 02 '11 at 14:29
0

The following code works for me

 <input type="text" name="email" placeholder="E-Mail ID" autofocus></input>
Ronald P Mathews
  • 2,178
  • 1
  • 22
  • 26