Flash CS3 Documentation
ActionScript 2.0 Language Reference > ActionScript classes > TextField > replaceSel (TextField.replaceSel method)
replaceSel (TextField.replaceSel method)
public replaceSel(newText:String) : Void
Replaces the current selection with the contents of the newText parameter. The text is inserted at the position of the current selection, using the current default character format and default paragraph format. The text is not treated as HTML, even if the text field is an HTML text field.
You can use the replaceSel() method to insert and delete text without disrupting the character and paragraph formatting of the rest of the text.
You must use Selection.setFocus() to focus the field before issuing this command.
Note: This method will not work if a style sheet is applied to the text field.
Availability: ActionScript 1.0; Flash Player 6
Parameters
newText:String - A string.
Example
The following example code creates a multiline text field with text on the Stage. When you select some text and then right-click or Control-click over the text field, you can select Enter current date from the context menu. This selection calls a function that replaces the selected text with the current date.
this.createTextField("my_txt", this.getNextHighestDepth(), 10, 10, 320, 240);
my_txt.border = true;
my_txt.wordWrap = true;
my_txt.multiline = true;
my_txt.type = "input";
my_txt.text = "Select some sample text from the text field and then right-click/control click "
+ "and select 'Enter current date' from the context menu to replace the "
+ "currently selected text with the current date.";
var my_cm:ContextMenu = new ContextMenu();
my_cm.customItems.push(new ContextMenuItem("Enter current date", enterDate));
function enterDate(obj:Object, menuItem:ContextMenuItem) {
var today_str:String = new Date().toString();
var date_str:String = today_str.split(" ", 3).join(" ");
my_txt.replaceSel(date_str);
}
my_txt.menu = my_cm;