0
votes

I have three text boxes on the stage id=red, blue, green same as the keys in my cars Object/Array

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/halo" creationComplete="carsToBox()">
    <fx:Script>
        <![CDATA[
            public function carsToBox():void{

                var cars:Object={red:"300zx",blue:"Skyline",green:"Supra"};
                    for(var tempObj:String in cars)
                    {
                        tempObj.text= cars[tempObj];
                    }
            }       
        ]]>
    </fx:Script>

    <s:TextInput x="65" y="53" id="red"/>
    <s:TextInput x="64" y="88" id="blue"/>
    <s:TextInput x="64" y="118" id="green"/>
</s:WindowedApplication>

So I'm thinking "tempObj.text" would equal red.text but I can't stick "tempObj" with ".text" is there a way this can be done?

1

1 Answers

1
votes

Basically your tempObj is actually the property names of the cars object. I've renamed it to prop for clarity. As the property names of cars is the same os the property names of the textInput controls you can use that name like so:

<?xml version="1.0" encoding="utf-8"?>
    <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                           xmlns:s="library://ns.adobe.com/flex/spark" 
                           xmlns:mx="library://ns.adobe.com/flex/halo" creationComplete="carsToBox()">
        <fx:Script>
            <![CDATA[
                public function carsToBox():void
                {

                    var cars:Object={red:"300zx",blue:"Skyline",green:"Supra"};
                    for(var prop:String in cars)
                    {
                        this[prop].text = cars[prop]
                    }
                }       
            ]]>
        </fx:Script>

        <s:TextInput x="65" y="53" id="red"/>
        <s:TextInput x="64" y="88" id="blue"/>
        <s:TextInput x="64" y="118" id="green"/>
    </s:WindowedApplication>