NanoHead
NanoHead

Reputation: 159

How to access dynamic TextArea values in AS3?

I have created a dynamic TextArea and want retrieve the value and display in the trace. To run the code below you must drag the TextArea component into the stage.

import fl.controls.TextArea; 
var totalTextArea=5;
     //Create multiple textarea
for(var a:int = 0; a<totalTextArea; a++){
        var cta:TextArea = new TextArea(); 
        cta.move(300,0+(a*100)); 
        cta.setSize(100, 60); 
        cta.condenseWhite = true; 
        cta.htmlText = a+'TextAreaA TextAreaB TextAreaC TextAreaD';  
        cta.name="TA"+a
        addChild(cta);
        trace(cta.text)
        cta.addEventListener(MouseEvent.CLICK, ShowCurrentValue);

}
//accessing TextArea 
function ShowCurrentValue(evt:MouseEvent):void{
        for(var b:int = 0; b<totalTextArea; b++){
            trace("somethingheere.txt")
        }

Upvotes: 0

Views: 875

Answers (1)

David Rettenbacher
David Rettenbacher

Reputation: 5120

I suppose you're clicking in the TextArea.

function ShowCurrentValue(evt:MouseEvent):void
{
    var textArea:TextArea = (evt.target as TextArea);
    trace(textArea.name + ": " + textArea.htmlText);        
}

If you want to list all textareas then I suggest you to put them into an array during creation and iterate over the array:

import fl.controls.TextArea; 
var totalTextArea = 5;
var textAreas:/*TextArea*/Array = [];

//Create multiple textarea
for(var a:int = 0; a<totalTextArea; a++)
{
    var cta:TextArea = new TextArea(); 
    cta.move(300,0+(a*100)); 
    cta.setSize(100, 60); 
    cta.condenseWhite = true; 
    cta.htmlText = a+'TextAreaA TextAreaB TextAreaC TextAreaD';  
    cta.name="TA"+a
    addChild(cta);
    trace(cta.text)
    cta.addEventListener(MouseEvent.CLICK, ShowCurrentValue);

    // add to array
    textAreas.push(cta);
}
//accessing TextArea 
private function ShowCurrentValue(evt:MouseEvent):void
{
    for(var b:int = 0; b < textAreas.length; b++)
    {
        var textArea:TextArea = textAreas[b] as TextArea;
        trace(textArea.name + ": " + textArea.htmlText);
    }
}

Upvotes: 1

Related Questions