Set cursor position in WebTextArea

Is there any way to set the cursor position e.g. to the end of a text block inside a WebTextArea?

cheers
®

Not natively, AFAIK. But you can call the JS method “Input.setSelectionRange” which takes two arguments, the start and the end. To place the cursor, you’ll simply want to pass the same parameter twice. For the end of the text, this refers to the length of the text.

I am not in front of my Xojo machine but it’d be something along these lines:

Dim pos as string = str(me.Text.len) me.ExecuteJavascript "Xojo.get('"+me.ControlID+"').setSelectionRange("+pos+", "+pos+");"

Untested!!

Thanks, Alex. It did not work, but you pointed me in the right direction… What worked (sort of):

me.ExecuteJavascript "document.getElementById('" + me.ControlID + "_inner').setSelectionRange("+pos+", "+pos+")"

Now I just need to have the text area scroll down and I’m almost happy.

It should?! Hm, have you tried different browsers? Might be a browser bug after all…

What worked for me (Safari 7.0.2) is this:

me.ExecuteJavascript "document.getElementById('" + me.ControlID + "_inner').setSelectionRange("+pos+", "+pos+")" me.ExecuteJavascript "var height = document.getElementById('" + me.ControlID + "_inner').scrollHeight; document.getElementById('" + me.ControlID + "_inner').scrollTop = height "

Hm strange. However glad you worked it out. Gonna do a blog post in the next couple of days I suppose.

I should have cleaned up a bit…:

[code] Dim pos as string = str(me.Text.len)

//me.ExecuteJavascript “document.getElementById(’” + me.ControlID + “_inner’).select();”
dim theScript as string

theScript = “var theElement = document.getElementById(’” + me.ControlID + “_inner’);” + _
“theElement.setSelectionRange(”+pos+", “+pos+”);" + _
“var height = theElement.scrollHeight;” + _
“theElement.scrollTop = height”

me.ExecuteJavascript theScript[/code]

That works quite nicely.

In your snippet, the “_inner” part was missing. Adding this made it work too:

Dim pos as string = str(me.Text.len) me.ExecuteJavascript "Xojo.get('"+me.ControlID+"_inner').setSelectionRange("+pos+", "+pos+");"

great, i want read from htmlviewer, i tray this:
me.ExecuteJavascript “document.getElementById(’” + TextArea1.ControlID + “_inner’).value = document.getElementById(’” + HTMLViewer1.ControlID + “_inner’).value;”

why cannot run that sript?
the message error is:
Could not execute returned javascript: Cannot read property ‘value’ of null
Source: document.getElementById(‘AkvnTAV7_inner’).value = document.getElementById(‘JmG4hnqe_inner’).value;

[quote=131631:@husnul yaqin]great, i want read from htmlviewer, i tray this:
me.ExecuteJavascript “document.getElementById(’” + TextArea1.ControlID + “_inner’).value = document.getElementById(’” + HTMLViewer1.ControlID + “_inner’).value;”

why cannot run that sript?
the message error is:
Could not execute returned javascript: Cannot read property ‘value’ of null
Source: document.getElementById(‘AkvnTAV7_inner’).value = document.getElementById(‘JmG4hnqe_inner’).value;[/quote]

Please stop hijacking other threads like that. Your question has absolutely nothing to do with topic. You got an answer in the thread you posted with exactly the same question a few minutes earlier :
https://forum.xojo.com/15950-capture-webhtmlviewer

I just wanted to post a caution, in testing a column of numeric inputs I discovered that if every control has this in the GotFocus event; there is a possibility of a feedback loop. This is caused when the JavaScript on the client is still executing when the next control is clicked, you’ll end up with multiple controls flashing as each of them selects all, then the next, and next again.

I corrected this by adding a timer to the window with a timeout set to 250 (milliseconds), it works in a local environment, but may need to be greater if the internet connection is slow; or maybe it doesn’t matter; maybe it is entirely related to the amount of time it takes for the client browser to execute the JavaScript. Anyway, the timer clears a flag and each GotFocus control event must check the flag before ExecuteJavaScript. If the time has not lapsed, I just don’t execute and wait for the user to settle down. It now pains the ten-keyers who want to enter the whole row in 2 seconds…

[quote=69325:@Roland Vögtli]Dim pos as string = str(me.Text.len)
me.ExecuteJavascript “Xojo.get(’”+me.ControlID+"_inner’).setSelectionRange("+pos+", “+pos+”);"[/quote]

What is this “Xojo.get” ? Here it triggers an error :

Could not execute returned javascript: Xojo.get is not a function

However, this works :

Dim pos as string = str(TextField1.Text.len) TextField1.ExecuteJavascript "document.getElementById('"+TextField1.ControlID+"_inner').setSelectionRange("+pos+", "+pos+");"