Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getElementById equivalent in Actionscript?

I want to dynamically access a bunch of objects in my mxml. I can construct their name/id. In Javascript I can do getElementById(ID) to get the object. How can I do this in Actionscript?

I really can't do getChildByName because it is too cumbersome: I have access to object A, which has a child B, which has a child C, which have the children D, E & F (their names are related to A's name). I want to get D, E & F. For getChildByName, it seems I have to use A to get B, then get C, and then get D, E & F. And if add a new parent to B (change the mxml hierarchy), then the code will break... And I really don't want to do that.

Any advice? Thx!

Update: What I am asking is, how do I access object D given its name and/or id, both of which are strings.

like image 663
sc45 Avatar asked Aug 10 '09 18:08

sc45


2 Answers

Since the children are actually like "properties" of the document, you should be able to do something like this:

var elem:Type_of_E = this["constructed_id_of_E"];

If you are in a subdocument of the document just use parentDocument["constructed_id_of_E"] to get the element.

like image 63
Cristian Ivascu Avatar answered Oct 12 '22 19:10

Cristian Ivascu


Actually, once you assign them an ID, they become a public member variable of the given element. So for instance if you have

<mx:Canvas><mx:Label id="myLabel" /></mx:Canvas>

You can access it by it's id as a variable name:

myLabel.text = "Hello world!";

Let me know if you need more clarification. If this isn't what your asking, feel free to comment and I'll take another whack at it.

Regards, Chris

Update: All mxml components are accessible this way, regardless of how many parents they have

like image 30
Chris Thompson Avatar answered Oct 12 '22 20:10

Chris Thompson