Table of Contents

TextField.onSetFocus( ) Event Handler Flash 6

callback invoked when the field gains focus
theField.onSetFocus(oldFocus)

Arguments

oldFocus

The TextField, MovieClip, or Button object that previously had focus, or null if no object was previously focused.

Description

The onSetFocus( ) event is triggered when theField gains focus. A text field is said to have focus when it contains the text insertion point or when characters within it are selected. A field gains focus when:

When a text field's selectable property is false, the field cannot be focused by the user, but it can be focused programmatically with Selection.setFocus( ).

The following code registers a callback function that responds to onSetFocus( ):

// Create a text field that accepts user input
this.createTextField("theField_txt", 1, 0, 0, 200, 20);
theField_txt.type = "input";
theField_txt.border = true;
// Assign the onSetFocus() callback using a function literal
theField_txt.onSetFocus = function (newFocus) {
  trace(this + " now has focus. Old focus was " + newFocus);
};

Notice that from the body of the callback we can refer to theField_txt using the this keyword. To stop a callback from handling the onSetFocus( ) event, use the delete operator, as in:

// Make sure to omit the () operator after the function name!
delete theField_txt.onSetFocus;

To capture all focus events centrally, rather than for a single instance, use Selection.onSetFocus( ).

Example

The following code uses onSetFocus( ) and onKillFocus( ) to highlight a field with color when it is focused, providing a nice interface cue for the user:

this.createTextField("userName_txt", 1, 0, 0, 200, 20);
userName_txt.type = "input";
userName_txt.border = true;
userName_txt.background = true;
userName_txt.backgroundColor = 0xA4660B;
userName_txt.onSetFocus = function (oldFocus) {
  this.backgroundColor = 0xF5BF70;
}
userName_txt.onKillFocus = function (newFocus) {
  this.backgroundColor = 0xA4660B;
}

To apply this effect to all text fields in a movie, use:

TextField.prototype.onSetFocus = function (oldFocus) {
  this.backgroundColor = 0xF5BF70;
}
TextField.prototype.onKillFocus = function (newFocus) {
  this.backgroundColor = 0xA4660B;
}

See Also

Selection.onSetFocus( ), Selection.setFocus( ), TextField.background, TextField.backgroundColor, TextField.onKillFocus( ), TextField.selectable, TextField.tabEnabled, TextField.tabIndex, TextField.type; Chapter 10


Table of Contents