Kishor Kumar
Kishor Kumar

Reputation: 543

Finding target's id when its parent generates event in Flex

Here I'm trying to get the id of the target in showMe(). For example, when I click button or image panel's showMe() function executes first. Here I want to capture the button/image's id.

private function init():void
            {
                pnl.addEventListener(MouseEvent.MOUSE_DOWN,showMe);
            }

            private function showMe(eve:MouseEvent):void
            {
                Alert.show("Hello");
            }

    <s:Panel id="pnl" height="400" width="700" creationComplete="init()">
                <s:Button id="btn1" label="showParents"/>
                <mx:Image id="img1" source="markupIndicator.png"  buttonMode="true"/>

    </s:Panel>

Any help is appreciated.

Upvotes: 1

Views: 1935

Answers (2)

laurent
laurent

Reputation: 90776

I think that should work:

private function init():void
    {
        pn1background.addEventListener(MouseEvent.MOUSE_DOWN,showMe);
            btn1.addEventListener(MouseEvent.MOUSE_DOWN,showMe);
            img1.addEventListener(MouseEvent.MOUSE_DOWN,showMe);
    }

    private function showMe(eve:MouseEvent):void
    {
        trace(eve.currentTarget.id);
    }

    <s:Panel id="pnl" height="400" width="700" >
                <s:Button id="btn1" label="showParents"/>
                <mx:Image id="img1" source="markupIndicator.png"  buttonMode="true"/>
                <s:Panel id="pn1background" height="400" width="700" / >
    </s:Panel>

Upvotes: 2

Constantiner
Constantiner

Reputation: 14221

It is not very clear why you need to catch panel's mouseDown and try to get inner control's ids but you can change your code this way:

private function init():void
            {
                pnl.addEventListener(MouseEvent.MOUSE_DOWN,showMe);
            }

            private function showMe(eve:MouseEvent):void
            {
                Alert.show("Hello " + eve.currentTarget.id);
            }

            private function showMyID(eve:MouseEvent):void
            {
                showMe(eve);
            }

    <s:Panel id="pnl" height="400" width="700" >
                <s:Button id="btn1" label="showParents" click="showMyID(event)"/>
                <mx:Image id="img1" source="markupIndicator.png" click="showMyID(event)" buttonMode="true"/>

    </s:Panel>

Upvotes: 3

Related Questions