This code doesn’t replace anything, it adds a letter probably entered from keyboard in the place where the caret is at the moment… May it be you wanted to get selection boundaries?
var tf:TextField = new TextField();
tf.width = 200;
tf.height = 20;
tf.border = true;
tf.type = 'input';
addChild(tf);
stage.addEventListener(KeyboardEvent.KEY_DOWN, handleTextInput);
function handleTextInput(evt:KeyboardEvent):void {
if(evt.keyCode == Keyboard.ENTER){
var startSelection:int = tf.selectionBeginIndex;
var endSelection:int = tf.selectionEndIndex;
var totalToReplace:int = endSelection - startSelection;
var repArray:Array = new Array(totalToReplace);
repArray.forEach(toAsterisk);
tf.replaceSelectedText(repArray.join(''));
}
}
function toAsterisk(o:*, i:int, a:Array):void {
a* = '*';
}
Not even close .
I mean on method replaceText () .
So the reference say this :
replaceText(beginIndex:int, endIndex:int, newText:String):void
Replaces the range of characters that the beginIndex and endIndex parameters specify with the contents of the newText parameter.
and Author code is above ; so I am confused how can same name represent beginIndex and endIndex .
The same way as you can do array.splice(1,0,‘new array element’) // this won’t delete any elements from array, but add one more at the second position. Or ‘abcd’.replace(’’,‘f’) //will add ‘f’ to the beginning of the string etc…
If it says beginIndex should be integer and endIndex should to be integer it doesn’t mean they have to be different, they may be the same as wel… =\