user2862947
user2862947

Reputation: 1

To get text from textfield

I am adding a textfield in a movieclip and adding that to a backround bg.Here is the code for that::

                        box = new boxMc();
                        bg.addChild(box);
                        box.x=boxX;
                        box.y=boxY;

                        exampleText="<p class='sarath'>"+k+"</p>";
                        boxVal = new TextField()
                        box.addChild(boxVal);
                        boxVal.styleSheet=sheet;
                        boxVal.htmlText=exampleText;
                        boxX=boxX+28        
                        boxArray.push(box)

Now i want to retrive the text from the textbox boxVal..and I tried as follow::

for(var j:Number =0;j<boxArray.length;j++)
                {
                    var mc:MovieClip = boxArray[j] as MovieClip;
                    trace(mc.getChildAt(1).text)
}

when am tracing mc.getChild(1),it is displaying as TEXTFIELD,but trace(mc.getChildAt(1).text) gives me the following error

1119: Access of possibly undefined property text through a reference with static type flash.display:DisplayObject.

Upvotes: 0

Views: 185

Answers (1)

moosefetcher
moosefetcher

Reputation: 1901

putvande is right, but you may want to know why.

getChild returns a DisplayObject and DisplayObjects do not have a text property. You need to 'cast' the returned DisplayObject to one of its subclasses (in this case TextField) which DOES have a text property.

Effectively, TextField(mc.getChildAt(1)) 'converts' the DisplayObject returned by getChildAt() to a TextField. You can then happily access .text and any of the other TextField properties.

Upvotes: 1

Related Questions