Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I focus on an input field when a phonegap page loads?

I would like to focus on a text input in a PhoneGap application when the page loads. I have tried the MooTools version $('entry').focus(); and document.getElementById('entry').focus() when the DOM is ready.

This works fine when the HTML page is viewed in a normal web browser, but in the iPhone simulator running PhoneGap, it doesn't work.

Is there a way to focus on a form field for iPhones that forces the virtual keyboard to appear?

like image 616
827 Avatar asked Apr 03 '11 04:04

827


People also ask

How do you make an input field focused?

To set focus to an HTML form element, the focus() method of JavaScript can be used. To do so, call this method on an object of the element that is to be focused, as shown in the example. Example 1: The focus() method is set to the input tag when user clicks on Focus button.

How do you focus an input box in HTML?

Input Text focus() MethodThe focus() method is used to give focus to a text field. Tip: Use the blur() method to remove focus from a text field.


2 Answers

As of iOS 6, the UIWebView class has a property called "keyboardDisplayRequiresUserAction". If you want to programmatically focus text form elements, set this to false via your phonegap config.xml:

<preference name="KeyboardDisplayRequiresUserAction" value="false" />

The default value is true.

More here: http://community.phonegap.com/nitobi/topics/calling_focus

like image 116
adamvert Avatar answered Sep 18 '22 20:09

adamvert


As far as I know, this is an intentional limitation of WebKit on both iPhone and Android; only a user touch action can trigger a keyboard. I'm still trying to concoct a workaround, but I'm not very optimistic.

like image 38
Luke Dennis Avatar answered Sep 19 '22 20:09

Luke Dennis