Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

is there a way to get the cursor position index of html text field

Explorer ,
Mar 20, 2007 Mar 20, 2007
I'm trying to work on a text pad like MC with "insert text at position X" and "find and replace" can anyone tell me how to find the cursor index of an html text field. Selection.getBeginIndex() , Selection.getEndIndex(), Selection.getCaretIndex() as far as I know these methods only work with text and not htmltext.
What I'm trying to do is assign different parts of the string to variables so that I can combine them to reconstitute my html string.
TOPICS
ActionScript
592
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Mentor , Mar 21, 2007 Mar 21, 2007
enfantterrible:

If I understand correctly, you want the corresponding position in the
htmlText propery of the caret index (which as you point out is only the
position in the plain text version of the textfield content).

Below is how I would do it. Maybe there's an easier or more efficient
way (using XML objects etc), but I don't know it. You could conduct all
your string searches in the plain text and then convert the result to
the html positions. I didn't build this to accept negative ...
Translate
Mentor ,
Mar 21, 2007 Mar 21, 2007
enfantterrible:

If I understand correctly, you want the corresponding position in the
htmlText propery of the caret index (which as you point out is only the
position in the plain text version of the textfield content).

Below is how I would do it. Maybe there's an easier or more efficient
way (using XML objects etc), but I don't know it. You could conduct all
your string searches in the plain text and then convert the result to
the html positions. I didn't build this to accept negative parameters
for the pos parameter (e.g. from end of string backwards) but the
function could be adapted to do this too if necessary. The test data is
not proper html from a text field, but it doesn't really matter what it
is so long as its formatted correctly. It just separates the tags from
the content to enable apples with apples comparison.


//some test data
var testStringXML = "<format>Hel<b>lo i can't t<i>ell you h</i>ow hap</b>py I am.</format>";
var undecoratedString = "Hello i can't tell you how happy I am.";


//the conversion function
function findDecoratedPosition(pos:Number, xmlString:String, test:Boolean):Number {
//anything between '<' and '>' should be excluded from plain text search.
//create an array of array elements (n=2) consisting of tag then plain text
//requires correctly formatted xml/html
var tempArr:Array = xmlString.split("<");
for (var aa = 0; aa<tempArr.length; aa++) {
tempArr[aa] = tempArr[aa].split(">");
}
//remove the first and last elements of the new array (these are empty)
tempArr.pop();
tempArr.shift();
//find the corresponding plaintext location and calculate the "decorated" position
var retPos:Number = 0;
var posCount:Number = 0;
for (var aa = 0; aa<tempArr.length; aa++) {
retPos += tempArr[aa][0].length+2+tempArr[aa][1].length;
//+2 allows for missing < and >
posCount += tempArr[aa][1].length;
if (posCount>pos) {
return (retPos+(pos-posCount));
}
}
//out of range
return -1;


}


//test code:
for (var aa = 0; aa<undecoratedString.length; aa++) {
var altPos = findDecoratedPosition(aa, testStringXML);
trace(undecoratedString.charAt(aa)+" in position "+aa+" plainText equates to position "+altPos+" in the decorated version ["+testStringXML.charAt(altPos)+"]");

}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 18, 2009 Apr 18, 2009
LATEST

Hi!

I used your AS to get the cursor position index of html text field, but I found a big problem with it! If I have an input textfiled with a simple text (not HTML) and an empty row in it, your code have a problem to get the cursor position in the HTML code! How can I solve this problem? I know your message is very old, but the solving of this problem is very, very important for me.

Thank you, WISION

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Mar 21, 2007 Mar 21, 2007
Oh, and you can remove the boolean 'test' parameter... forgot to do that sorry.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 21, 2007 Mar 21, 2007
GWD
thanks for the input and the brillant piece of logic I was able to adapt it to fit my needs.

Once again thanks!!
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines