5 Replies Latest reply on May 2, 2007 8:48 AM by vezey

    document.getElementById() equivalent?

    vezey Level 1
      Is there an equivalent in Actionscript 3.0 to the Javascript method document.getElementById()? It looks like you have to recursively check parents and children through the entire application tree. And I am sooooo lazy, and really don't want to write this method.

      Thanks in advance, Bill
        • 1. Re: document.getElementById() equivalent?
          peterent Level 2
          What are you trying to accomplish? If you have a component (say a VBox) with id="box" and inside of it you have a Button with id="mybutton", then you can get the Button via box.mybutton.
          • 2. Re: document.getElementById() equivalent?
            vezey Level 1
            This flex app is almost entirely Actionscript, and is has several dynamically-generated panel objects, each with it's own set of nested children. Among those children is some checkboxes. Now, when an action is performed in one panel, I want to find a checkbox in one of the other panels and enable/disable.

            Maybe I'm missing something here (usually the case), but my experience is that when the id or name of an object is dynamically generated in an class, rather than in a repeater in the mxml, you can't retrieve that element by simpley calling box.mybutton, but you have to walk the tree up and down, looking for that named or id'd object. I sure hope I'm wrong, but I will play again with the guidance you have sent.

            Did I say thank you for replying? Thank you for replying.

            • 3. Re: document.getElementById() equivalent?
              peterent Level 2
              If you have ActionScript code that creates a component you could assign it an id and keep a map of ids-to-components in the code. For example:

              var b:Button = new Button();
              b.id = "helpButton";

              var map:Object = new Object();
              map["helpButton"] = b;
              map["choiceCombo"] = c;

              Now when you want a specific control and know its id, you can get it through the map. If all of your components follow this pattern you should be able to get a component very quickly.
              • 4. Re: document.getElementById() equivalent?
                vezey Level 1
                I very much appreciate your help.

                Your approach of mapping ids makes sense. I had also thought of storing the entire name path from the parentApplication to the lowest component on the tree for each component, and using string manipulation and a series of getChildByName() calls.

                But here's the rub. In ActionScript, it looks like ids assigned dynamically in an class are treated differently than ids created in mxml. When I attempt to call an id in ActionScript that is created dynamically in a class, I get an undefined property error. For example, the class creates a CheckBox:

                var paperAddCheckBox:CheckBox = new CheckBox();
                paperAddCheckBox.id = "add" + paperId;
                paperAddCheckBox.x = 12;
                paperAddCheckBox.y = 27;

                And the chekcbox is then added to the the panel or what have you. Later, in a function I want to find this chekcbox and perform some action on it, like set enabled = true or false. To test whether I can actually get to the checkbox, by saying something like:


                I get the following error (using sdk. compiling on the command line):

                Error: Access of undefined property add662

                So, I have a feeling I am being dense, and am missing something obvious that would allow me to access this component by id. I am poised to write a recursive function that will allow me to find any element in an application by id or name, but if this functionality is built into Flex, I would be dancing in the streets. Completely dressed. As a favor to my neighbors, who have had enough of my foolishness.

                Thanks so much,
                • 5. Re: document.getElementById() equivalent?
                  vezey Level 1

                  Oh my hat. I get it. Of course, you call map["helpButton"].myProperty. And that works.

                  You have no idea how much that helps. Should have thought of that myself, blah blah blah.

                  Have a blessed day, Peter.