How can you autofocus on the form field in iPhone Safari?

I am trying to have a specific form field focus automatically in the Safari browser on the iPhone. I thought it would be pretty straightforward, but I can’t get it to work. So, is this really impossible, or am I missing something super-obvious (which I suspect)? Here is the code I'm using:

<!DOCTYPE html> <html> <head> <script type="text/javascript"> function formfocus() { document.getElementById('element').focus(); } window.onload = formfocus; </script> </head> <body> <form> <input/> <input id="element" autofocus/> <input/> </form> </body> </html> 

You can see that I am trying to get focus twice, once through js and once using the HTML attribute "aufofocus" in the input field. And the trick too. This works on desktop browsers, but when I open the page on my iPhone 4, there are no cubes, that is, I have to set the focus manually by clicking on one of the form fields.

Ultimately, I would like to open the website in a mobile browser and focus on the form field, including the keyboard open to start typing.

Thanks for any help!

+14
javascript html5 iphone mobile-safari focus
Apr 24 '11 at 16:35
source share
3 answers

According to this page , autofocus is not supported on iphone / ipad for usability reasons.

+13
Sep 15 '11 at 17:53
source share

Try entering an input element of type

 <input type="email" id="element" autofocus/> 

and to return you can use

 <script type="text/javascript"> if (!("autofocus" in document.createElement("input"))) { document.getElementById("element").focus(); } </script> 
0
Apr 24 '11 at 16:41
source share

Try using .focus () inside setTimeout ().

 setTimeout(function() { jQuery('#element').focus(); }, 500); 

Hope it will work for everyone.

-one
Feb 06 '15 at 7:05
source share



All Articles