Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

The caretPos object and selecting text in a TextArea

Posted on 2004-08-08
10
Medium Priority
?
775 Views
Last Modified: 2012-06-27
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.

0
Comment
Question by:sforcier
  • 6
  • 2
10 Comments
 
LVL 25

Expert Comment

by:devic
ID: 11746270
hi sforcier,
try this:
==================================
<html>
<head>
<script>
var curObj;
function setPos(obj)
{
      if(obj.createTextRange)
      {
            curObj=obj;
            curObj.curPos = document.selection.createRange().duplicate();
      }
}
function insertMyVal()
{
      if(curObj && curObj.curPos)
      {
            with(curObj.curPos)
            {
                  text=" " + curObj.form.myval.value + " ";
            }
      }
      else
      {
            alert("click in text area first")
      }
}
</script>
</head>
<body>
<form>
      <textarea style=width:300px;height:200px; onclick="setPos(this)" onkeyup="setPos(this)">one two, one two, check the microphone
      </textarea><br><br>
      <input name=myval style=width:300px; value="[devic]"><br>
      <input type=button style=width:300px; value="inset my text" onclick="insertMyVal()">
</form>
</body>
</html>
0
 
LVL 6

Author Comment

by:sforcier
ID: 11746320
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.

0
 
LVL 25

Expert Comment

by:devic
ID: 11746439
look at window status bar:

======================
<!-- ms demo script
     ref: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwebteam/html/webteam12032001.asp
-->
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JScript">
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 = elem.value.search( bookmark );
    window.status = "Caret is at character " + i;
    elem.value = orig;
  }
}
</SCRIPT>
</HEAD>

<BODY>
<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)">
</BODY>
</HTML>
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 6

Author Comment

by:sforcier
ID: 11747093
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.
0
 
LVL 6

Author Comment

by:sforcier
ID: 11747185
Correction, it causes the caret become the *entire* TextArea.
0
 
LVL 6

Author Comment

by:sforcier
ID: 11747243
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.
0
 
LVL 6

Author Comment

by:sforcier
ID: 11751892
I have found the solution to the first question on my own. The object is named TextRange, not caretPos.
http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/obj_textrange.asp
0
 
LVL 6

Author Comment

by:sforcier
ID: 11751903
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.
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 12108799
PAQed, with points refunded (350)

Computer101
E-E Admin
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question