I am trying to highlight some text in a WebTextArea using some java script that I found. It does not work. Can someone tell me what I am doing wrong. Here is the method called from a search text field.
ControlID doesnât always map to the actual DOM object. To find the actual object use the Developer Tools in your browser. When you write JavaScript like this, you are going outside the framework, so Xojo isnât responsible if the hack ever breaks.
For a WebTextArea, the actual inner DOM object is ControlID + "_field".
The controlID seems to change every time I view the Textarea. The control name stays the same âTaLogâ. I just want to highlight some text. Is there an easier way to do that?
I am using IndexOf to fill the StartPos variable. The further down the text in the textarea that the searched word is the further off the start position of the word is. Here is the code in the Search field.
Var searchWord As String
Var currentPosition As Integer = 0
searchWord = me.Text
currentPosition = TaLog.Text.IndexOf(searchWord)
If currentPosition > 0 Then
HighlightText(currentPosition - 1, searchWord.Length)
Else
MessageBox("Word not found.")
End If
Here is a simple test web app. Search for the word âLocationâ. The textarea is readonly, but you can click in the textarea and hit backspace, then the search works correctly every time.
I copied the string from a simple text file. testsearchTA.zip (8.4 KB)
I only have a why but not a solution, maybe you can figure it out.
Why is this happening?
Because there are 16 EndOfLines before âLocationâ, the selected text is 16 characters off.
You may be able to count the EndOfLines before the search you need, or find another JavaScript function that works for this case.
Thank you so much Tim for taking the time build a test app with the workaround.
This seems to work perfectly in my app.
I donât think that I would have figured that code out on my own.
Also thank you to AlbertoD for trying to lead me in the right direction.
You are welcome.
Following Timâs code, the problem is the 0D0A for each end of line.
You can fix your code by simple adding Opening event to TaLog and use this code: