8.7. Retrieving User Input

Problem

You want to retrieve the text that a user has entered into a text field, text input, or text area instance for further processing.

Solution

Use ActionScript to access the value by way of the instance’s text property.

Discussion

You must use ActionScript in order to retrieve the value from an input text field, text input, or text area. However, the code that is required is short and simple. You can access the text value using the instance name and its text property.

This following code writes the value of an input text field to the Output window when you are testing your movie. The text field in this example has an instance name of tInputField. In order for this code to work, it must be placed on a frame in the same timeline in which the text field exists:

	trace (tInputField.text);

Text that you retrieve from an input text field, text input, or text area is always a string value. The string data type can perform differently in some operations than a number data type. For example, when you use the plus sign with two numbers, they are added together. But when you use the plus sign with two strings, the values are appended to one another. Therefore, if you want to perform mathematical operations on a value that you retrieve from an input text field, you should convert that value to a number data type. You can do this very quickly using either the parseInt( ) or parseFloat( ) functions.

The parseInt( ) function takes a string value, and returns an integer (a ...

Get Flash 8 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.