WebTextField select text

Hi all.
Is possible to select all text inside a webtextfield when I trigger “gotfocus” event? In desktop textfields I have sellength method for that…

Many thanks

As far as I know this is not possible with the standard WebTextField control.

In the gotFocus event use ExecuteJavascript method and pass the following string:
document.getElementById("<>").select();
Where the <> is replaced by the control’s Id.

A little precision :slight_smile:

WebTextField1.ExecuteJavascript(“document.getElementById(’”+WebTextField1.ControlID+"_inner’).select();")

I like putting methods like this in a ControlExtension Module

Sub selectAll(extends wtf as WebTextField) wtf.ExecuteJavaScript("document.getElementById('"+wtf.ControlID+"_inner').select();") End Sub

That way you can call “.selectAll” on any webtextfield, you could also add this for a webtextarea as well.

You’re all fantastic! Great tip!

This is great, thanks for posting - this works great on desktop browsers. Does anyone know how to get this working on an iPad? It doesn’t seem to work. Any help much appreciated.

Hello!
I not that good in javascript, but what I need is to inserttext(pos,text) at the cursor position. If a set is marked or selected, replace it with the new text. The problem is to determine pos.
Regards

If you’re using HTML Edit you can use the SelText property to insert text where you need it just like the desktop control :slight_smile:

Thanks but, it is not working for me. As i told you, I am new to javascript, always programmed with VB.
The issues are:

FIRST

iResult=  ExecuteJavaScript(iJavaScript)

ExecuteJavaScript does not return any value, so
even if it works, the value stays in javascript

SECOND
Every time i call this procedure an error rises
with the following message:

code of the sub is

function mLocate(pWebTextArea as web) as string
  dim iJavaScript as string
  dim iResult as string
  
  'addto is equivalent to iJavaScript=iJavaScript + "text"
 ' where the false parameter does not reset the variable
 ' and the true parameter adds a newline

  AddTo iJavaScript,"function FromWebTextAreaSelected()",false,true
  AddTo iJavaScript,"{",false,true
  AddTo iJavaScript,"var ctl = document.getElementById('" + pWebTextArea.ControlID + "');",false,true
  AddTo iJavaScript,"var response = " + chr(34) + chr(34) + ";",false,true
  AddTo iJavaScript,"var startPos = ctl.selectionStart;",false,true
  AddTo iJavaScript,"var endPos = ctl.selectionEnd;",false,true
  AddTo iJavaScript,"response = startPos + " + chr(34) + ":" + chr(34) + "endPos;",false,true
  AddTo iJavaScript,"alert(response);",false,true
  'AddTo iJavaScript,"return response;",false,true       'no return can be captured
  AddTo iJavaScript,"}",false,true

  'iResult = ExecuteJavaScript(iJavaScript)  'does not work
  ExecuteJavaScript(iJavaScript)
  
  return iResult
end function

(original from Brock Nash)

The caller
is a botton1.action and the webtextarea is txtSourceCode, and it is called as follows:

dim iResult as string
  iResult = mLocation(txtSourceCode)

Regards