views:

49

answers:

3

I'm trying to get the sibling of an mxml tag similar to the way siblings are selected in javascript. Is this possible in Actionscript?

For example, when I click the TextArea with id textarea1, I need it to tell me that the sibling has an id of rect1 so I can do further processing to it.

<s:Group>
     <s:TextArea id="textarea1" click="getSibling(event)" />
     <s:Rect id="rect1" />
</s:Group>
+1  A: 

My initial thought here is to access the parent and then retrieve a list of children within it.

function getSibling(e:Event):void { 
   //get an array of children from the parent.
   var children:Array = e.target.parent.getChildren();  

   //process children as you wish... 
}

This was discussed with respect to Javascript here.

Hope this helps.

Nick

nickgs.com

direct
+1  A: 

As far as I know there is no way to do this. However, both textarea1 and rect1 are children of the Group. If you give the group an ID you should be able to loop over all the children to find all the siblings of the TextArea.

In Flex 3, you'd use a for loop, numChildren, and getChildAt. I suspect in Flex 4 it would be similar.

www.Flextras.com
+2  A: 

Assuming Group, TextArea and Rect are UIComponents, I think this should work:

    private function getSibling(e:Event):void {
        var parent:UIComponent = e.currentTarget.parent;

        if(parent) {
            var len:int = parent.numChildren;
            var child:UIComponent;
            for(var i:int = 0; i < len; i++) {
                child = parent.getChildAt(i) as UIComponent;
                if(child && child != e.currentTarget) {
                    trace(child.id);
                }
            }
        }
    }
Juan Pablo Califano
+1 for having the code.
www.Flextras.com