Javascript – Show keyboard for div with contenteditable=”true” in ios

contenteditableiosjavascript

I have a div with contenteditable="true". How can I set focus on this div via javascript(if it possible on pure js, without jquery)?

Best Solution

Prior to iOS 6 this wasn't possible, but thankfully they've changed that. You can't do it in general for a website, but if you are embedding a UIWebView in your application you can do it now:

UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
webView.keyboardDisplayRequiresUserAction = NO;

Then your element.focus() will work and bring up the keyboard as desired.

http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIWebView_Class/Reference/Reference.html#//apple_ref/occ/instp/UIWebView/keyboardDisplayRequiresUserAction

More info on what changed with iOS 6 & WebKit:

http://developer.apple.com/library/ios/#releasenotes/General/RN-iOSSDK-6_0/_index.html#//apple_ref/doc/uid/TP40012166-CH1-SW19