Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set text for a textfield in UIWebView

I have a UIWebView and I want to set one of it's text field to certain text. Is that even possible? If so, how can I do that? Thanks

like image 478
Dexter Avatar asked May 22 '11 18:05

Dexter


2 Answers

Yes you can set the text for a textfield in the UIWebView using javascript

As an example if you have a textfield in this form

<input id="textFieldID" type="text" value="TextValue"/>

Setting value:

[theWebView stringByEvaluatingJavaScriptFromString:@"document.getElementById('textFieldID').value = 'Hello World'"]

Getting value:

NSString* value = [theWebView stringByEvaluatingJavaScriptFromString:@"document.getElementById('textFieldID').value"];
like image 68
Cyprian Avatar answered Nov 20 '22 10:11

Cyprian


In Swift-5 with WKWebView use this

To set text field

webView.evaluateJavaScript("document.getElementById('textFieldID').value = 'Hello'") { (result, error) in
   print(result) //This will Print Hello
}

To get the value of text field

webView.evaluateJavaScript("document.getElementById('textFieldID').value") { (result, error) in
   print(result) //This will Print value of text field
}
like image 43
Avneesh Agrawal Avatar answered Nov 20 '22 08:11

Avneesh Agrawal