Skip to main content
Known Participant
February 24, 2014
Answered

Multiple choice checkboxes

  • February 24, 2014
  • 1 reply
  • 629 views

Hi

I was wondering if anyone help me.

I hav a mutliple choice question where you have to select 4 answers.

How can I disable the submit button until 4 checkboxes have been selected?

Here is my code so far:

submitButton.addEventListener(MouseEvent.CLICK, testHandler);

function testHandler(event:MouseEvent):void

{

     var minAnswers:int = 4;

     var maxAnswers:int = 4;

     var selectedAnswers = 0;

     var totalCheckboxes:int = 6;

     for (var checkboxNumber:int = 1; checkboxNumber <= totalCheckboxes; checkboxNumber++)

     {

          if (this['answer_' + checkboxNumber].selected == true)

          {

               // increment selected answers

               selectedAnswers++;

          }

     }

     if ( selectedAnswers >= minAnswers && selectedAnswers <= maxAnswers)

     {

                    if((answer_1.selected) && (answer_2.selected) && (answer_4.selected) && (answer_6.selected)){

                              testtext.text = "Thats correct!.";

                              }

     }

     else

     {

                    testtext.text = "Thats incorrect";

     }

}

Can anyone help me?

Many thanks

This topic has been closed for replies.
Correct answer robdillon

I think this will do what you want, if I'm reading you right:

------------------

// make an array of the question checkboxes...

var qList:Array = new Array(answer_1,answer_2,answer_3,answer_4);

submitButton.addEventListener(MouseEvent.CLICK,testHandler);

// then in the button function count the number of checkboxes that have been selected and do one thing or another based on the count...

function testHandler(event:MouseEvent):void {

          var thisMany:int = 0;

          for (var i:String in qList) {

                    if (qList.selected) {

                              thisMany ++;

                    }

          }

          if(thisMany == 4) {

                    //do something

          } else {

                    // do something else

          }

}

----------------------

1 reply

robdillon
robdillonCorrect answer
Participating Frequently
February 24, 2014

I think this will do what you want, if I'm reading you right:

------------------

// make an array of the question checkboxes...

var qList:Array = new Array(answer_1,answer_2,answer_3,answer_4);

submitButton.addEventListener(MouseEvent.CLICK,testHandler);

// then in the button function count the number of checkboxes that have been selected and do one thing or another based on the count...

function testHandler(event:MouseEvent):void {

          var thisMany:int = 0;

          for (var i:String in qList) {

                    if (qList.selected) {

                              thisMany ++;

                    }

          }

          if(thisMany == 4) {

                    //do something

          } else {

                    // do something else

          }

}

----------------------

dips045Author
Known Participant
February 25, 2014

Many thanks Rob thats perfect! thank you