8.27. Responding to User Text Entry

Problem

You want to perform a task when the content of a text field is modified by user input.

Solution

Assign a function definition to the text field’s onChanged( ) event handler method. Alternatively, you can use a listener object with a defined onChanged( ) method and register it with the text field using addListener( ).

Discussion

You can specify actions to be performed each time the content of a text field is changed by user input, whether that change be deleting or cutting characters, typing in characters, or pasting characters. You can define the text field’s onChanged( ) event handler method:

myTextField.onChanged = function (  ) {
  trace("the value has been modified");
};

When a user makes any change to the value of an input text field with an onChanged( ) handler defined, the actions defined in the onChanged( ) handler are executed.

You can also define an onChanged( ) method for a listener object (or listener objects). If multiple objects need to be updated when a change occurs in the input text field’s value, you should register those objects as listeners. You should define an onChanged( ) method for the listener object and then register the listener with the text field’s addListener( ) method. The addListener( ) method takes a reference to the listener object. For example:

myListener = new Object(  );
myListener.onChanged = function (  ) {
  // Actions go here.
};
myTextField.addListener(myListener);

You can use addListener( ) to register as many ...

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.