Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
144
rated 0 times [  151] [ 7]  / answers: 1 / hits: 6462  / 3 Years ago, thu, april 1, 2021, 12:00:00

I am trying to understand the architecture in WebView2. With WebBrowser i used to get an attribute from a result returned by the GetElementById as follows: Document.GetElementById("DropDownList").GetAttribute("selectedIndex")


I know that the ExecuteScriptAsync in WebView2 can run a javascript and return a result as a string. However, it looks like that it does not know how to get an attribute from an element. The below code returns a null. Although, the getElementById returns the correct result.

ExecuteScriptAsync("document.getElementById('DropDownList').getAttribute('selectedIndex')")


Is my syntax incorrect? How to get an attribute in WebView2?. Do we have to write a function in the script and call it from the host?


Thanks


More From » webview2

 Answers
4

A HTMLSelectElement does not have an attribute called.'selectedIndex'. It has a PROPERTY called 'selectedIndex'.


Call it like this from WebView2:


await ExecuteScriptAsync("document.getElementById('DropDownList').selectedIndex");

[#1547] Sunday, March 28, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jenamackennac

Total Points: 304
Total Questions: 110
Total Answers: 107

Location: Ecuador
Member since Thu, Jun 4, 2020
4 Years ago
jenamackennac questions
;