Skip to main content
October 20, 2009
Answered

How can I find out how many checkboxes are checked, please!

  • October 20, 2009
  • 2 replies
  • 850 views

Hello everyone,

I have been at it for hours and pretty much cannot think straight and would highly apprecaite your help on this. I have an array called myArr which has 5 elements in it. I then generate checkboxes and the number of checkboxes are matched with the myArr.length(); I would like to be able to find out what checkboxes are check/selected before moving to the next screen. User will need to click on a "NEXT' button in order to move to the next screen. Next button is disabled until user selects the checkbox(s).

here is the code:

var cb:CheckBox;

captionArr:Array = new Array('img1' , 'img2', 'img3', 'img4', 'img5');

myArr:Array = new Array('c1', 'c2', 'c3', 'c4', 'c5');

for(var i:Number=0; i < myArr.length; i++)

{

   cb = new CheckBox();

  cb.y = i * 25;

  cb.label = captionArr;         //  CHECKBOX LABELS

  addChild(cb);

  cb.addEventListener(MouseEvent.CLICK, clickHandler);

}

function clickHandler(e:MouseEvent):void

{

     if(e.target.selected)

      {

         trace(''SELECTED");

      }

    else if(!e.target.selected)

   {

     trace("NOT SELECTED");

   }

}

Thanks.

This topic has been closed for replies.
Correct answer kglad

use checkCBF():

var cb:CheckBox;

captionArr:Array = new Array('img1' , 'img2', 'img3', 'img4', 'img5');

myArr:Array = new Array('c1', 'c2', 'c3', 'c4', 'c5');

var cbA:Array=[];

for(var i:Number=0; i < myArr.length; i++)

{

   cb = new CheckBox();

  cb.y = i * 25;

  cb.label = captionArr;         //  CHECKBOX LABELS

cbA.push(cb);

  addChild(cb);

  cb.addEventListener(MouseEvent.CLICK, clickHandler);

}

function clickHandler(e:MouseEvent):void

{

     if(e.target.selected)

      {

         trace(''SELECTED");

      }

    else if(!e.target.selected)

   {

     trace("NOT SELECTED");

   }

}

function checkCBF(){

for(var i:Number=0; i < cbA.length; i++){
if(cbA.selected){
//cbA selected
} else {
//cbA not selected
}
}

}

2 replies

Inspiring
October 21, 2009

In addition to the suggestion to loop through the check boxes to see which ones are checker, I would like to suggest you use Event.CHANGE instead of MouseEvent.CLICK. Event.CLICK is more indicative of the property you are dealing with than CLICK.

for(var i:int = 0; i < myArr.length; i++) {
     cb = new CheckBox();
     cb.y = i * 25;
     cb.label = captionArr;
     addChild(cb);
     cb.addEventListener(Event.CHANGE, onCheckBoxChange);

}

function onCheckBoxChange(e:Event):void {
     if (e.target.selected) {
          trace("SELECTED");
     }
    else(!e.target.selected) {
          // you don't need else if - it is either or
          trace("NOT SELECTED");
   }
}

October 21, 2009

Guys,

Thanks you so much for your help.

kglad
Community Expert
Community Expert
October 21, 2009

you're welcome.

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
October 20, 2009

use checkCBF():

var cb:CheckBox;

captionArr:Array = new Array('img1' , 'img2', 'img3', 'img4', 'img5');

myArr:Array = new Array('c1', 'c2', 'c3', 'c4', 'c5');

var cbA:Array=[];

for(var i:Number=0; i < myArr.length; i++)

{

   cb = new CheckBox();

  cb.y = i * 25;

  cb.label = captionArr;         //  CHECKBOX LABELS

cbA.push(cb);

  addChild(cb);

  cb.addEventListener(MouseEvent.CLICK, clickHandler);

}

function clickHandler(e:MouseEvent):void

{

     if(e.target.selected)

      {

         trace(''SELECTED");

      }

    else if(!e.target.selected)

   {

     trace("NOT SELECTED");

   }

}

function checkCBF(){

for(var i:Number=0; i < cbA.length; i++){
if(cbA.selected){
//cbA selected
} else {
//cbA not selected
}
}

}