O'Reilly logo

Actionscript Cookbook by Joey Lott

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

8.23. Assigning Focus to a Text Field

Problem

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

Solution

Use the Selection.setFocus( ) method.

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.
Selection.setFocus("myTextField");
Selection.setFocus("_root.myTextField");

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:

Selection.setFocus("/myTextFieldVar");

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

Selection.setFocus(null);

See Also

Recipe 11.21

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required