Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do I specify cursor position in a textarea

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I have a problem. Here is where the problem is

-----SAMPLE CODE------------

<SCRIPT ID=clientEventHandlersVBS LANGUAGE=vbscript>
<!--

Sub Html_onclick
txtarea.value = area.value & &quot;</Html>&quot;
End Sub

-->
</script>

I have made an online html editor
Where i click a button and a html tag would apear in the textarea.

the problem is that i want to place the tag where i place the cursor. It is at the moment placing the text always at the end of the string.

How do i specify that it must paste the text where i place the cursor and give the textarea focus.

Please help me

 
Ahh.. You definitely have a problem. HTML text objects don't have text selection properties like VB text objects.

I can think of two workarounds, both of them ugly.

The first involves DHTML. You could capture the onMouseDown event of the text area. If you use a fixed width font (ie, courier) you can calculate the current row and column that the cursor is at, based upon the x and y offsets of the event object relative to the X and Y cooridinates and sizes of the textarea. Once you know the column and row, you can use createTextRange to convert the text of the area into a TextRange object. You can then manipulate the TextRange object to grab the left characters before the cursor position, add the inserted string, then add the right characters after the cursor, then replace the .text of the textrange with your new text.
One obvious problems with this approach include large fonts may screw up your location calculations.

Option two involves using the VB textbox as an ActiveX component for your editing box, instead of a <TEXTAREA>. That should support all the methods for text selection that you are looking for.

If you come up with any other ideas please share them.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top