Recipe 8.26 Responding When Text Is Selected or Deselected

8.26.1 Problem

You want to perform a task when a text field is selected or deselected.

8.26.2 Solution

Assign a function definition to the text field's onSetFocus( ) and/or onKillFocus( ) methods.

8.26.3 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 (  ) {   _root.myOutputField.text = "instructions for this form field"; };

A useful implementation of the onKillFocus( ) event handler method might be to check the value of a form field once it loses focus:

myInputField.onKillFocus = function (  ) {   if (this.text != "valid answer") {     _root.myOutputField.text = "invalid answer supplied";   } };


ActionScript Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2005
Pages: 425

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net