3 Replies Latest reply on May 7, 2010 8:10 AM by David_F57

    Referencing checkboxes using strings of their IDs

    Chris Callaghan Level 2

      I'm sure this is a really easy one (im a beginner)...


      I'm creating checkboxes at runtime and assigning IDs in the format "checkBox_1", "checkBox_2" etc.


      I'm also pushing these strings to an array, so I have a list of all the checkboxes I have made (across multiple NavigationContent containers)


      I need to loop through the array and convert the string into a valid checkbox reference and grab the label of that checkbox.


      What I'm struggling with is that the following throws an error



      var myCheckBox:CheckBox;

      myCheckBox = CheckBox("checkBox_1");




      and myCheckBox = "checkBox_1" as CheckBox; doesnt work



      I'm missing something really basic here

        • 1. Re: Referencing checkboxes using strings of their IDs
          Sree_D Level 2

          Try adding the checkbox item to the array instead of storing the Ids.


          If you want to store only ids then used get child methods to get the check box item.

          1 person found this helpful
          • 2. Re: Referencing checkboxes using strings of their IDs
            Alex Browning

            Unless you want to store the IDs in an array for a specific reason (in which case the advice above is valid - push the checkbox object to the array instead of just the ID) you can just loop through all the checkboxes on the canvas by doing something like this :-


            for each (var myCheckBox:CheckBox in yourContainer.getChildren())





            Hope that helps,



            • 3. Re: Referencing checkboxes using strings of their IDs
              David_F57 Level 5



              Pushing the object onto the array is the better option but you can also reference the object by name, either way you need to typecast any operations on the checkbox , If you decide to use the checkbox's name you need to retreive the displayobject first then you can use it as per the call to set the selected value shown below.




              protected function application1_creationCompleteHandler(event:FlexEvent):void


              for (var i:int=1; i < 11;i++)


              var cb:CheckBox = new CheckBox();



              cb.selected = false;

              cb.label = "Check no:"+String(i);








              private function setCheckBoxes(): void


              var tmpCB:DisplayObject;

              for (var i:int=0; i <arr.length;i++)


              tmpCB = cbCanvas.getChildByName(arr[i]);

              (tmpCB as CheckBox).selected=true;

              //(arr[i] as CheckBox).selected=true;