Web2.0 Insert Text at CursorSelection

Just wanted to share a helpful method that will insert text into a textArea where the cursor is in the field or it will replace the highlighted text with the text provided. Simply add the extension method into any global module. I’m currently using this to allow the user to insert “{{tokens}}” into a WebTextArea from dropdown list of tokens:

Public Sub InsertTextAtCursorSelection(extends c as WebTextArea, value as string)
  dim s as string = "var v = jQuery('#"+c.ControlID+"_field').val();"+EndOfLine+_
  "var cursorPos = jQuery('#"+c.ControlID+"_field').prop('selectionStart');"+EndOfLine+_
  "var cursorEndPos = jQuery('#"+c.ControlID+"_field').prop('selectionEnd');"+EndOfLine+_
  "var textBefore = v.substring(0,  cursorPos);"+EndOfLine+_
  "var textAfter  = v.substring(cursorEndPos, v.length);"+EndOfLine+_
  "jQuery('#"+c.ControlID+"_field').val(textBefore + '"+value.ReplaceAll("'","/'")+"' + textAfter);"
  c.ExecuteJavaScript(s)
End Sub

FeatureRequest to be made native:
feedback://showreport?report_id=63064

2 Likes

Very cool! Thank you!

Thank you!