Every time you create something with an id from actionscript, save it in a central dictionary. Then you only have to scan the dictionary.
________________________________ From: [email protected] [mailto:[EMAIL PROTECTED] On Behalf Of gaurav1146 Sent: Thursday, November 29, 2007 7:31 AM To: [email protected] Subject: [flexcoders] Re: Accessing Components By Dynamically generated IDs I was wondering if there was a by which I could access the componets directly using ids without iterating through all the children. I observed that the components that were present directly in the mxml can be accessed using this[$id] but the components that I am adding later using actionscript cannot be accessed using this[$id]. I have a main application which takes as input, name value pairs from users. Initially I show only two pairs of textboxes with ids as (field1,value1 and field2,value2). The user can add more textboxes by clicking on "Add Input" button which adds a component comprising of two textboxes with ids as field<i> and value<i>. When the user clicks on showAllFields() I try to iterate through all the field textboxes using this['field'+i]. The problem is that the main application is only able to access the first two textboxes that were present in the mxml earlier and is not able to acccess any of the textboxes that were added dynamically using ActionScript. Here is my code which consists of the main mxml file and component file. ----------- Main MXML file ----------------- <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml <http://www.adobe.com/2006/mxml> " layout="absolute"> <mx:Script> <![CDATA[ public var numInputs:Number=2; public function addInputs():void{ var tbi:TextboxInput = new TextboxInput(); parentBox.addChild(tbi); numInputs++; var parentHBox:HBox = tbi.getChildByName("hbox") as HBox; parentHBox.id = "hbox" + numInputs; (parentHBox.getChildByName("field") as TextInput).id = "field" + numInputs; (parentHBox.getChildByName("value") as TextInput).id = "value" + numInputs; } public function showAllFields():void{ var str:String; for(var i:Number = 1; i< numInputs + 1; i++){ if(i == 1){ str = this['field'+i].text; } else{ str += ","+this['field'+i].text; } } mx.controls.Alert.show(str); } ]]> </mx:Script> <mx:VBox> <mx:VBox id="parentBox"> <mx:HBox id="hbox1"> <mx:TextInput id="field1"/> <mx:TextInput id="value1"/> </mx:HBox> <mx:HBox id="hbox2"> <mx:TextInput id="field2"/> <mx:TextInput id="value2"/> </mx:HBox> </mx:VBox> <mx:Button click="addInputs()" label="Add Input"/> <mx:Button click="showAllFields()" label="Show all fields"/> </mx:VBox> </mx:Application> -------------Component File (TextboxInput.mxml) ------------- <?xml version="1.0" encoding="utf-8"?> <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml <http://www.adobe.com/2006/mxml> "> <mx:HBox name="hbox"> <mx:TextInput name="field"/> <mx:TextInput name="value"/> </mx:HBox> </mx:Canvas> ---------------------------------------------------------- Any pointers on what could the issue be would be greatly appreciated. Regards, Gaurav --- In [email protected] <mailto:flexcoders%40yahoogroups.com> , "Rafael Faria" <[EMAIL PROTECTED]> wrote: > > I got the same problem... and solve with this function > > > function $(object:String,container:DisplayObjectContainer = null) { > if (container == null) container = this; > > for (var i:uint=0; i < container.numChildren; i++) > { > var child:DisplayObject = container.getChildAt(i); > > if (child.name== object) { > return child; > } > > if (container.getChildAt(i) is DisplayObjectContainer) > { > var obj = $(object,DisplayObjectContainer(child)); > if (obj is DisplayObject) { > return obj; > } > } > } > } > > > > > try $('element name') > > > > > --- In [email protected] <mailto:flexcoders%40yahoogroups.com> , DreamCode <dreamcode@> wrote: > > > > I think you should be able to constuct the id something like this: > > > > var currentItem:Object = this['hbox' + number] > > > > I assume you know the number of boxes you create..... > > > > --Allan > > > > On Nov 21, 2007 11:49 PM, gaurav1146 <gaurav1146@> wrote: > > > > > > > > > > > > > > > Hi, > > > I have a component where a user can add text boxes and Combo boxes > > > depending on his requirement. For each addition I assign the ids to > > > the elements in ascending order. Now I want to read the values from > > > each of these textboxes and combo boxes in a for loop. How can I do > > > that. I have a structure something like: > > > > > > <mx:VBox> > > > <mx:HBox id="hbox1"> > > > <mx:Textbox id="textbox1"/> > > > <mx:ComboBox dataProvider="{dp}" id="comboBox1"> > > > <mx:HBox> > > > > > > <mx:HBox id="hbox2"> > > > <mx:Textbox id="textbox2"/> > > > <mx:ComboBox dataProvider="{dp}" id="comboBox2"> > > > <mx:HBox> > > > <!--Next HBox would be added here with id="hbox3" and so on --> > > > <mx:Button click="addHbox()"> > > > </mx:VBox> > > > > > > Had the number of HBoxes been fixed I could access them by > > > {hboxId.textboxid.text} but I can't do the same for dynamically > > > generated ids. I tried using getChildByName but that was not working > > > for me. Is there any function where I can get a component by supplying > > > its id(like Javascript's getElementById)? > > > > > > TIA, > > > Gaurav > > > > > > > > >

