Skip to main content
Participant
February 14, 2013
Question

How to code a button to check all input fields at once?

  • February 14, 2013
  • 1 reply
  • 1565 views

I have made an interactive math problem that has the user go through about a dozen steps.  One of the steps requires the user to complete the problem on their own.  In order to fill out the problem, I have created multiple input text fields so that they can be filled with the correct answers.  In this problem, there are about 23 of these text fields.  After all of these inputs are filled in, I would like to find a way to display the correct answes with the click of a button.  Once, the button is clicked, I would like to have the correct answers to have a highlight of green or some other indicator behind it to indicate to the user that it is correct.  I have tried to find an if statement to statsify these conditions but I have not been successful at doing so.  In other words, I know what I want to do but I do not know how to tell actionscript 3.0 and flash to do so.  Could someone please help me in figuring this out?

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
February 14, 2013

if your input textfields have reference names like input_tf1,input_tf2,..,input_tf23, you can use array notation to loop through them:

var i:int;

for(i=1;i<=23;i++){

if(this["input_tf"+i].text==answerA){  // where answerA is an array of the correct answers

this["input_tf"+i].filters=[glowFilter];  // create this glow filter

}

}

or, you can list your input textfields in an array and loop through them:

tfA=[input_tf1,input_tf2,...,input_tf23];

for(i=1;i<=23;i++){

if(tfA.text==answerA){  // where answerA is an array of the correct answers

tfA.filters=[glowFilter];  // create this glow filter

}

}

DMScltAuthor
Participant
February 15, 2013

I added my code that I am using now but obviously I do not want to go this certain route to check all the answers at once.  If you could please explain to me how some of your code would work as I only have basic experience with ActionScript. Also can I just Google how to create a glow filter?  I would appreciate your help.  Thank you

Code:

/*-------------show correct answers---------*/
fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredo);
function onehundredo(e:Event):void {
       if (fourteenthinput.text == "0") {
    gotoAndStop("label100") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}

fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredone);
function onehundredone(e:Event):void {
       if (fifteenthinput.text == "40.25") {
    gotoAndStop("label101") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}
fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredtwo);
function onehundredtwo(e:Event):void {
       if (sixteenthinput.text == "0") {
    gotoAndStop("label102") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}
fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredthree);
function onehundredthree(e:Event):void {
       if (seventeenthinput.text == "-52.5") {
    gotoAndStop("label103") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}
fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredfour);
function onehundredfour(e:Event):void {
       if (eighteenthinput.text == "26.25") {
    gotoAndStop("label104") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}
fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredfive);
function onehundredfive(e:Event):void {
       if (nineteenthinput.text == "-78.75") {
    gotoAndStop("label105") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredsix);
function onehundredsix(e:Event):void {
       if (twentyinput.text == "0") {
    gotoAndStop("label106") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredseven);
function onehundredseven(e:Event):void {
       if (twentyoneinput.text == "0") {
    gotoAndStop("label107") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredeight);
function onehundredeight(e:Event):void {
       if (twentytwoinput.text == "0") {
    gotoAndStop("label108") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundrednine);
function onehundrednine(e:Event):void {
       if (twentythreeinput.text == "40.25") {
    gotoAndStop("label109") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredten);
function onehundredten(e:Event):void {
       if (twentyfourinput.text == "0") {
    gotoAndStop("label110") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredeleven);
function onehundredeleven(e:Event):void {
       if (twentyfiveinput.text == "0") {
    gotoAndStop("label111") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredtwelve);
function onehundredtwelve(e:Event):void {
       if (twentysixinput.text == "-385") {
    gotoAndStop("label112") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredthirteen);
function onehundredthirteen(e:Event):void {
       if (twentyseveninput.text == "-8.75") {
    gotoAndStop("label113") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredfourteen);
function onehundredfourteen(e:Event):void {
       if (twentyeightinput.text == "227.5") {
    gotoAndStop("label114") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredfifteen);
function onehundredfifteen(e:Event):void {
       if (twentynineinput.text == "-175") {
    gotoAndStop("label115") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredsixteen);
function onehundredsixteen(e:Event):void {
       if (thirtyinput.text == "7") {
    gotoAndStop("label116") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredseventeen);
function onehundredseventeen(e:Event):void {
       if (thirtyoneinput.text == "140") {
    gotoAndStop("label117") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredeighteen);
function onehundredeighteen(e:Event):void {
       if (thirtytwoinput.text == "0") {
    gotoAndStop("label118") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundrednineteen);
function onehundrednineteen(e:Event):void {
       if (thirtythreeinput.text == "0") {
    gotoAndStop("label119") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredtwenty);
function onehundredtwenty(e:Event):void {
       if (thirtyfourinput.text == "40.25") {
    gotoAndStop("label120") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";


}
}
}fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredtwentyone);
function onehundredtwentyone(e:Event):void {
       if (thirtyfiveinput.text == "268800") {
    gotoAndStop("label121") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredtwentytwo);
function onehundredtwentytwo(e:Event):void {
       if (thirtysixinput.text == "2450") {
    gotoAndStop("label122") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredtwentythree);
function onehundredtwentythree(e:Event):void {
       if (thirtyseveninput.text == "97300") {
    gotoAndStop("label123") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}

kglad
Community Expert
Community Expert
February 15, 2013

you're checking 24 textfields with that code each time fourteenthreset_btn is clicked which doesn't make sense in your code.  in mine, it made sense.

in any case, you should be using arrays like i showed to condense that code to a few lines and you should explain what you're trying to check when that button is clicked.  or, did you mean to use a different button for each textfield?

also, you're nesting named functions which has no benefit and is always a bad idea.