Pahiro
Pahiro

Reputation: 21

Android Webview - Change Input Text

I've set up my webview properly and I execute my Javascript in the onPageFinished method.

The problem is that whenever I try to change the value of my input, no matter what I do, I end up with the entire webview being replaced by the value of the input that I'm trying to replace.

view.loadUrl("javascript:document.forms[0].elements['password'].value = 'test';");

Any ideas?

PS. This code in the same place works perfectly.

view.loadUrl("javascript:checkForm()");

This executes the checks and submits the form. I get a popup "Please enter a value" after running this.

I have also tried document.getElementsByName("password")[0].value = 'test'; Same result.

RESOLVED: It was the emulator causing the problem. I tested directly on my phone with USB debugging switched on and it works perfectly.

Upvotes: 2

Views: 5753

Answers (2)

Dmytro
Dmytro

Reputation: 19

You try to change the view, so you need to use:

.dispatchEvent(new Event('input')) ",null).

Try this:

view.evaluateJavascript("javascript:  " +
    "var password= document.getElementById('password'); " +
    "password.value = 'test'; " +
    "password.dispatchEvent(new Event('input')) ",null);

Upvotes: 2

ILovemyPoncho
ILovemyPoncho

Reputation: 2792

Your first line of code works for me using id or name attributes:

view.loadUrl("javascript:document.forms[0].elements['password'].value = 'test';");

the second line, works with getElementById but doesn't with getElementByName:

view.loadUrl("javascript:document.getElementByName('password').value = 'test';");

So, that's not the problem. Maybe if you post the rest of the code.

Upvotes: 2

Related Questions