8.26. Responding When Text Is Selected or Deselected
Problem
You want to perform a task when a text field is selected or deselected.
Solution
Assign a function
definition to the text
field’s onSetFocus( )
and/or
onKillFocus( )
methods.
Discussion
You should define the onSetFocus( )
method for
any text field for which you want certain actions to be performed
when focus is brought to the field. Likewise, you should define the
onKillFocus( )
method for any text field for
which you want certain actions to be performed when focus leaves the
field. These methods are invoked automatically when focus changes on
a text field, though by default the methods are undefined.
myTextField.onSetFocus = function (prevFocus) { trace(this._name + " selected, previously selected was " + prevFocus._name); }; myTextField.onKillFocus = function (newFocus) { trace(this._name + " deselected, new selection is " + newFocus._name); };
In each case, the method is automatically passed a parameter. In the
case of onSetFocus( )
, the parameter is a
reference to the object that previously had focus and for
onKillFocus( )
, the parameter is a reference to
the object that is gaining focus. In either case, you are not
required to handle this parameter or do anything with it, but it is
there if you want or need it.
A handy use of the onSetFocus( )
event handler
method might be to set the value of an instructional output
field’s text when a particular input field is
selected in a form:
myInputField.onSetFocus = function ( ) ...
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.