2

I am loading a webview form in my iOS app and I want to autofocus a textfield on the form. I tried

NSString *focus = [NSString stringWithFormat:@"document.getElementsByName('j_captcha')[0].focus()"];
[self.webView stringByEvaluatingJavaScriptFromString: focus];

But that doesnt seem to work. "j_captcha" is the name of the textfield. How can I achieve this?

tbag
  • 1,268
  • 2
  • 16
  • 34

1 Answers1

0

This did the trick for me

NSString *evalStr = [NSString stringWithFormat:@"setTimeout( function(){document.getElementsByName('j_captcha')[0].focus();},1000);"];
[webView stringByEvaluatingJavaScriptFromString:evalStr];

This post helped - https://stackoverflow.com/a/14268931/653978

Community
  • 1
  • 1
tbag
  • 1,268
  • 2
  • 16
  • 34