### Get & Set HTML Element Property

Example of getting & setting the property of an element (requires the JavaObject library).
The element used is a TextArea defined in Index.html with id="message".
HTML Code Index.html
<!DOCTYPE html>
<html>
<body>
<p>
Testing JavaScript to get the number of rows for an Element, eg TextArea.
</p>
<hr>
<textarea id="message">This is a Message.</textarea>
<hr>
</body>
</html>
B4J Code
'The WebView is defined in layoutfile main.bjl
`Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private WebView1 As WebView
End Sub`
`Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("main")
MainForm.Show
WebView1.LoadUrl("file:///" & File.Combine(File.DirApp, "index.html"))
End Sub`
`Sub WebView1_PageFinished (Url As String)
Log("wv_PageFinished :: Start")
'
'Elementid to read its property - see index.html; This ...
Dim elementid As String = "message"
'Property to read the value from
Dim property As String = "rows"
Dim propertyvalue As Object
'JavaScript to be executed
Dim script As String
'Assignt the webview to a JavaObject
Dim joWV As JavaObject = WebView1
'Define the javascript
script = $"document.getElementById("${elementid}").${property};"$
Log("Script = " & script)
'Run the javascript
propertyvalue = joWV.RunMethodJO("getEngine", Null).RunMethod("executeScript", Array As String(script))
Log("TextArea Rows = " & propertyvalue)
'
script = $"document.getElementById("${elementid}").value = 'This TextArea has ${propertyvalue} rows.';"$
Log("Script = " & script)
joWV.RunMethodJO("getEngine", Null).RunMethod("executeScript", Array As String(script))
Log("wv_PageFinished :: End")
End Sub`
Adding Rows to a TextArea
To build upon previous to set the content of a WebView, an example of adding row to a TextArea.
Dim content As String
'JavaSccript to be executed
Dim script As String
'Assignt the webview to a JavaObject
Dim joWV As JavaObject = WebView1
'Define the javascript
Dim sb As StringBuilder
sb.Initialize
For i = 0 To 9
sb.Append($"Line ${i}\n"$)
Next
content = sb.ToString
script = $"document.getElementById("${elementid}").value = '${content}';"$
Log("Script = " & script)
'Set the content
joWV.RunMethodJO("getEngine", Null).RunMethod("executeScript", Array As String(script))

Changed: , Category: WebView, File: T6C284M2129.htm, ID: 2129