Skip navigation
Yue_Hong
Currently Being Moderated

Reference to child element

Dec 17, 2013 6:19 AM

Tags: #air #flex #actionscript_3.0

How can we reference to an element which is added dynamically on runtime?

Example code:

<?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/mx" applicationComplete="init(event)">

 

<fx:Script>

<![CDATA[

    import mx.events.FlexEvent; 

    protected function init(event:FlexEvent):void {

        trace(button1.label); 

        var newbtn:Button = new Button();

        newbtn.label = "New Button";

        newbtn.id = "button3";

        newbtn.name = "button3"; 

 

        mygroup.addElement(newbtn); 

 

        trace(this["button3"].label);

    }

]]>

</fx:Script>

 

<s:HGroup id="mygroup">

    <s:Button id="button1" label="Button 1" />

    <s:Button id="button2" label="Button 2" />

</s:HGroup>

 

</s:WindowedApplication>

When I try to run above code, it dispatch error

Error #1069: Property button3 not found on project1 and there is no default value.

So, how can I call to the newly added button?

 

Thank you.

 
Replies

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points