The caretPos object and selecting text in a TextArea

Two questions:

1) I cannot find any references for the caretPos object. Could anyone provide a good link to an API for this object?

2) The problem I am trying to solve by using caretPos is that I need to make a non-WYSIWYG html editor. All of the end-users will be using IE 5.5+, so browser compatibility should not be an issue. My question is whether or not using caretPos is an appropriate strategy for solving this problem. I'm not against using some third party tool (I'd even pay if it's a good value), but I will need to be able to add proprietary non-HTML tags. So the tool will have to (at the very least) allow me to insert text at a location that I specify.

Who is Participating?
Computer101Connect With a Mentor Commented:
PAQed, with points refunded (350)

E-E Admin
hi sforcier,
try this:
var curObj;
function setPos(obj)
            curObj.curPos = document.selection.createRange().duplicate();
function insertMyVal()
      if(curObj && curObj.curPos)
                  text=" " + curObj.form.myval.value + " ";
            alert("click in text area first")
      <textarea style=width:300px;height:200px; onclick="setPos(this)" onkeyup="setPos(this)">one two, one two, check the microphone
      <input name=myval style=width:300px; value="[devic]"><br>
      <input type=button style=width:300px; value="inset my text" onclick="insertMyVal()">
sforcierAuthor Commented:
Thanks for the post. I have some code that does that already.

The reason I need the API is because I need to make this function work:

//This function returns "length" number of characters that *preceed* the selected text in the object "taObj"
function LeftOfTAPosition(taObj, length){
//Hey, how do I do this??

However I will be making many similar text manipulation functions, so access to an API will be very helpful.

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

look at window status bar:

<!-- ms demo script
function saveCaret(elem)
  if ( elem.isTextEdit )
    elem.caretPos = document.selection.createRange();
function getCaretPos(elem)
  if ( elem.isTextEdit && elem.caretPos )
    var bookmark = "~";
    var orig = elem.value;
    var caretPos = elem.caretPos;
    caretPos.text = bookmark;
    var i = bookmark );
    window.status = "Caret is at character " + i;
    elem.value = orig;

<INPUT NAME="txtInput" ONSELECT="saveCaret(this)"
   ONCLICK="saveCaret(this)" ONKEYUP="saveCaret(this)" VALUE="Where are you?">
<INPUT TYPE="button" VALUE="caret pos" ONCLICK="getCaretPos(txtInput)">
sforcierAuthor Commented:
Thanks devic, however this code presents a problem because it moves the caretPos object to the end of the text. At least in my code.
sforcierAuthor Commented:
Correction, it causes the caret become the *entire* TextArea.
sforcierAuthor Commented:
I appreciate the assistance provided, though I would like to reiterate my basic questions.

1) A link to an API of caretPos (doesn't have to be official, just something that tells me what the methods/properties/etc. are)
2) Suggestions on my overall strategy given my needs (in the original post).

I've increased the points significantly because it seems this is more difficult than I initially expected.
sforcierAuthor Commented:
I have found the solution to the first question on my own. The object is named TextRange, not caretPos.
sforcierAuthor Commented:
The second question, "Is this a good strategy?" is still open for responses. If none are provided, I will request that the question be closed.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.