Recipe 8.23 Assigning Focus to a Text Field

8.23.1 Problem

You want to use ActionScript to bring focus to a text field.

8.23.2 Solution

Use the Selection.setFocus( ) method.

8.23.3 Discussion

You should use the Selection class's static getFocus( ) method to assign focus to a specific text field programmatically. The method assigns focus to the object whose name is passed in as a parameter. You should pass getFocus( ) a string value that evaluates to the path to the object. You can specify the parameter as an absolute or relative path, such as:

// The following are equivalent, assuming that myTextField exists within _root.

Additionally, to support Flash 4 and Flash 5 movies, the parameter passed to setFocus( ) can be a string that evaluates to the path of the variable associated with the text field, and the path can be given in slash notation if desired. For example:


To remove focus from a text field, you should call the setFocus( ) method and pass it the null value:


8.23.4 See Also

Recipe 11.21

    Part I: Local Recipes
    Part II: Remote Recipes