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 thatmyTextFieldexists 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
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access