O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Modifying a Text Field's Content

Once a text field's text content has been set, it can be reassigned via the text variable. For example, the following code creates a TextField object and sets it text content to "Hello":

var t:TextField = new TextField();
t.text = "Hello";

The following code completely reassigns t's text to the string "Goodbye":

t.text = "Goodbye";

To add new text following a text field's existing text (rather than completely reassigning the text field's text), we use either the TextField class's instance method appendText( ) or the += operator. For example, the following code adds the string "...hope to see you again!" to the text "Goodbye":

t.appendText("...hope to see you again!");

After the preceding line of code executes, t.text has the value:

"Goodbye...hope to see you again!"

The appended text adopts the formatting of the last character in the text field, and any existing text retains its original formatting. If the text field contains no text when appendText( ) is called, the appended text is formatted with the text field's default text format.

Tip

For information on text formatting and the default text format, see the next section, "Formatting Text Fields."

Like the preceding code, the following code adds new text at the end of a text field's existing text, but does so with the += operator rather than appendText( ):

t.text += " Come again soon.";

Unlike appendText( ), the += operator resets the formatting of all text in the text field to the default text format. The ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required