ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers

Problem

You want ActionScript to set the insertion point for a text field.

Solution

Use the TextField.setSelection( ) method.

Discussion

You can use TextField.setSelection( ) to set the cursor position in a text field by setting the beginning and ending index parameters to the same value. This example sets the cursor position in the text field, assuming it has focus:

// Positions the insertion point before the first character field.setSelection(0, 0);

You can retrieve the index of the cursor position with the read-only caretIndex property:

trace(field.caretIndex);

See Also

Recipe 9.21

Категории