Andrea Silvestri
Andrea Silvestri

Reputation: 1134

Flex getElementByName

I know that there is no such function as getElementByName in Flex but I also now that you can do this["object_id"] to get the element of the application u're in. What about getting an element inside another element? I've tried making element["id"] ? But in my try-catch it always runs the "catch" part =\ So: how do I get an element inside another element just having it's id in dynamically created string form?

Thank you in advance

Upvotes: 0

Views: 3738

Answers (3)

Ken Fehling
Ken Fehling

Reputation: 2111

If you're looking for the element in a Group, you can use this function:

static function getElementByName(group:GroupBase, name:String):IVisualElement {
    const child:DisplayObject = group.getChildByName(name);
    const index:int = group.getChildIndex(child);
    return group.getElementAt(index);               
}

Upvotes: 0

Manish
Manish

Reputation: 3522

If I understand correctly, you're asking for a way to get all the "elements" in a Flex application that have a certain name.

Here's an example (Flex 3):

<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*">
  <mx:Script>
    <![CDATA[
      private function testIt():void
      {
        var arr:Array = getDisplayObjectsByName(this, "foo");
        for each (var e:* in arr)
          trace(e);
      }

      private static function getDisplayObjectsByName(node:DisplayObjectContainer,
          name:String, recurse:Boolean = true):Array
      {
        var n:int = node.numChildren;
        var a:Array = [];
        for (var i:int = 0; i < n; i++) {
          var c:DisplayObject = node.getChildAt(i);
          if (c.name == name)
            a.push(c);

          if (recurse) {
            if (c is DisplayObjectContainer)
              a = a.concat(getDisplayObjectsByName(DisplayObjectContainer(c),
                  name, true));
          }
        }

        return a;
      }
    ]]>
  </mx:Script>
  <mx:VBox name="foo">
    <mx:HBox>
      <mx:Button name="foo" label="Test" click="testIt()" />
    </mx:HBox>
  </mx:VBox>
  <mx:Label text="Ignore Me" />
  <mx:VBox name="bar">
  </mx:VBox>
</mx:Application>

Here we're looking for all the elements called "foo" when the user clicks the "Test" button.

Output:

main0.foo
main0.foo.HBox5.foo

You'll notice that getDisplayObjectsByName() is static. All it does is traverse the display list (depth-first) and pick out all the objects with the specified name.

Upvotes: 0

weltraumpirat
weltraumpirat

Reputation: 22604

It depends on what kind of element you are trying to access.

  1. A child display object can be accessed by calling DisplayObjectContainer#getChildByName:
    element.getChildByName("name");

  2. A public variable (which could be set to also contain a child display object) can be accessed by using bracket syntax:
    element["name"];

    or simply using dot syntax:element.name

(where name is the name of the property you are trying to access).

Note that any instance you drag to the stage in the Flash IDE will automatically be assigned to a public variable, if you have the "automatically declare stage instances" option checked in your export settings. That is why using this[name]works.

Upvotes: 3

Related Questions