Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
105
rated 0 times [  112] [ 7]  / answers: 1 / hits: 8705  / 10 Years ago, tue, july 15, 2014, 12:00:00

I understand how to set the value of a edit box in WebView, and in a browser on PC with Javascript.



It is basically first find out the ID of the edit box (text), then set the value using:
On PC:



document.getElementById('xxxxxxxx').value = 'test'


In Android:



mWebView.loadUrl(javascript: document.getElementById('xxxxxxxx').value = 'test');


This script works in PC, but when I call the script in WebView.loadUrl, it just override my entire page to test.



It is weird to me because the following android code works on the radio button, but when it comes to the edit box, it just fails.



mWebView.loadUrl(javascript: document.getElementById('xxxxxxxx').checked = true);


Could you guys help me analyze what could be the possible reason? Thank you so much.



To make it clear, I am running the javascript code in WebViewClient.onPageFinished(WebView view, String url). So the WebView should be fully loaded. That also makes sense because I can check the radio button.


More From » android

 Answers
4

The problem is solved. It was because of the way I call javascript in java code.



code like



mWebView.loadUrl(javascript: [your javascript code]);


sometimes work, but it doesn't work in all the case. At least it doesn't work for the following case when I tried to set the textbox in a webview:



mWebView.loadUrl(javascript: document.getElementById('xxxxxxxx').value= 'test');


but it should work if you call the javascript as a function:



mWebView.loadUrl(javascript: (function() {document.getElementById('xxxxxxxx').value= 'test';}) (); );


That fixed my problem. Hope it is helpful.


[#43857] Tuesday, July 15, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kiarra

Total Points: 202
Total Questions: 111
Total Answers: 109

Location: Sudan
Member since Mon, May 31, 2021
3 Years ago
;