简体   繁体   中英

Get value from DIV element

I want to pass a HTML element value to an Android application. I

I tried retrieving the value using the code below:

document.getElementById(\"summary\").value --> giving null value
document.getElementById(\"summary\").innerHtml --> giving null value
document.getElementById(\"summary\").textContent --> giving null value 

I'm trying to implement javascript MutationObserver. So whenever there is a change in the content, I can immediately pass the value to the android java code.

Can anyone help me?

I used the following code:

    myWebView = (WebView)this.findViewById(R.id.myWebView);
    myWebView.getSettings().setJavaScriptEnabled(true);


     myWebView.loadUrl("https://helloworld.com/details");

    myWebView.addJavascriptInterface(new JavaScriptInterface(this, myWebView), "MyHandler");
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        myWebView.setWebContentsDebuggingEnabled(true);
    }

    myWebView.evaluateJavascript("(function(){return document.getElementById(\"summary\").textContent})();",
            new ValueCallback<String>() {
                @Override
                public void onReceiveValue(String html) {
                   Log.e("HTML Content",html);
                }
            });

This is my HTML code:

 <div class="well" id="summary" *ngIf="viewModel != null">
    <div class="SuccessSummary">{{viewModel.successSummary}}</div>
 </div>

Try this

myWebView.evaluateJavascript("(function(){return 
document.getElementById('summary').innerHTML})();",
        new ValueCallback<String>() {
            @Override
            public void onReceiveValue(String html) {
               Log.e("HTML Content",html);
            }
        });

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