[ Team LiB ] |
Recipe 8.26 Responding When Text Is Selected or Deselected8.26.1 ProblemYou want to perform a task when a text field is selected or deselected. 8.26.2 SolutionAssign a function definition to the text field's onSetFocus( ) and/or onKillFocus( ) methods. 8.26.3 DiscussionYou 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"; } }; |
[ Team LiB ] |