user767985
user767985

Reputation:

FIll in text fields in a UIWebView by element name

I'd like to write an iPhone app that packs the contents of a website into the app. First problem: entering the user data. I've tried the following:

[webView stringByEvaluatingJavaScriptFromString:@"document.username.text.value='testUser'"];

I have verified that I can run javascript at that time, a simple alert() works. However, I did not have any succes filling in that text field. The text field does not have an ID assigned, just a name (username). I did not find examples on the internet with an object that does not have an ID, and because I'm not familiar with javascript at all, I need your help. Thanks in advance!

Upvotes: 0

Views: 1992

Answers (2)

Mattias Wadman
Mattias Wadman

Reputation: 11425

If you want to only use DOM methods you probably need to do it like this, assuming it is the first form in the document and the input is named "username":

document.forms[0].username.value = "testUser"

Or if you add id="username" to the input element you can do:

document.getElementById("username").value = "testUser"

But also make sure to evaluate the script after the UIWebView has finished loading so that the DOM is completely loaded and ready for manipulation, i would recommend to use the webViewDidFinishLoad: delegate method in the UIWebViewDelegate protocol.

Upvotes: 1

techfoobar
techfoobar

Reputation: 66663

Use $('input[name="name"]').text('Hello');

If you are not using jQuery, you can do:

var myInput = table.getElementsByTagName("input");   
for (var i = 0; i < myInput.length; i++) {   
    if(myInput[i].getAttribute("name")=="name") {
        myInput[i].value = "Hello";  
        break;
    }
}

Change "name" and "Hello" as needed. :)

Upvotes: 0

Related Questions