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 thatmyTextField
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
Get Actionscript Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.