1 Reply Latest reply on Jul 22, 2007 3:29 AM by Greg Dove

    checkboxes

    phil1943 Level 1
      hello, do checkboxes have to have event listeners attached to them to detect if clicked or not ?

      Do I have to assign an individual event listener to every checkbox ?
      Is there an easier way ( my page has 31 individual checkboxes)

      thanks for any help.
        • 1. Re: checkboxes
          Greg Dove Level 4
          Yes they do. If you're referring to the v2 components, its all here:

          http://livedocs.adobe.com/flash/9.0/main/00002611.html#wp3651692

          yes you do need to assign a listener to each check box. But you can do that in a very simple way via a loop if you have an enumerable naming convention. i.e. e.g. chk_1, chk_2 etc.
          If not, you could just put all your checkboxes in an array and loop through the array.
          var myCheckBoxes =[likesChocolate,hatesBrocolli, likesSpam, prefersCocaCola] etc

          //common listener
          var checkBoxListener:Object= new Object();
          checkBoxListener.click = function(evt:Object){
          trace(evt.target+" was clicked");
          trace("it is now "+ ((evt.target.selected)? "selected" : "not selected"));
          }

          //examples of the two methods
          //first method:naming - here the example assumes names are chk_1, chk_2 etc
          //and the code is running in the same timeline that the chkboxes are attached to
          for (var i=1;i<=31;i++) {
          this["chk_"+i].addEventListener("click",checkBoxListener);
          }

          //second method:array of names of checkboxes

          var myCheckBoxes =[likesChocolate,hatesBrocolli, likesSpam, prefersCocaCola] ;

          for (var j=0;j<myCheckBoxes.length;j++) {
          myCheckBoxes[j].addEventListener("click",checkBoxListener);
          }

          I just typed these in quickly here... so if there are any typos please excuse them.