简体   繁体   中英

How to Change `<input>` element attributes(e.g. value) from objective-C in wkWebview/webView

Ok, I Found Certain links from this site,

but the issue is what if the tag doesn't consist id or name?

<input type="email" class="_style_3vhmZK" placeholder="Email: name@example.com" value="" data-reactid="49">

I tried like this:

TRY Number 1:

 NSString *javaScript1 = @"var passFields = document.document.getElementsByTagName('input'); \
            passFields[0].setAttribute('value','Naman')";   [webView evaluateJavaScript:javaScript1 completionHandler:nil];

TRY Number 2

 NSString *javaScript = @"document.getElementsByTagName('input')[0].setAttribute('value','Naman Vaishnav');";    [webView evaluateJavaScript:javaScript1 completionHandler:nil];

But still not achieve Target, Enlighten me if I'm making any mistakes...

Done Via Tags , Welcome to more sufficient way if there is any ,

  NSString *javaScript1 = @"var passFields = document.getElementsByTagName('input'); \
           for (var i = passFields.length>>> 0; i--;) { if(passFields[i].placeholder == 'Email: name@example.com'){ passFields[i].value ='%@';}}";

            NSString *javascriptWithCSSString = [NSString stringWithFormat:javaScript1,emailInfo[0]];
            [webView evaluateJavaScript:javascriptWithCSSString completionHandler:nil];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM