13.11. Retrieving Text Field, Text Input, and Text Area Values
Problem
You want to retrieve the value from a text field, text input, or text area.
Solution
Use the instance’s text property.
Discussion
Whether you are using a text field, text input, or text area, you can retrieve the value in the same way. Each of these text form controls has a text property that returns the current text value. For example, the following retrieves the text value from a text input with an instance name of ctiUsername and then saves it to a variable named sUsername:
var sUsername:String = ctiUsername.text;
Keep in mind that you’ll most often want to retrieve the value from a text form control only after the user has clicked on a submit button. Therefore the ActionScript code should be placed within a click( ) method on a listener object or within a button’s onRelease( ) event handler method. The following example uses a listener object registered with a button instance named cbtSubmit. When the user clicks on the button, the value that the user has entered into the ctiUsername text input gets displayed in the Output panel while you are testing the movie:
var oListener:Object = new Object();
oListener.click = function(oEvent:Object):Void {
trace(ctiUsername.text);
};
cbtSubmit.addEventListener("click", oListener);Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access