Skip to main content
Participant
August 2, 2018
Answered

For loop when reading inputs from checkboxs

  • August 2, 2018
  • 1 reply
  • 602 views

Greetings All,

So the premise is I'm trying to create a print button which checks which buttons are checked off, then prints off the ones which are checked off.

I've got the following code so far:

//The following code outputs which page numbers to print based off of checkbox into Field "Print_pages"

//all checkboxes are named "print1", "print2", print3", etc.

//initial page which will always print

var output = "[0,0]";

event.value = output;

//conditional pages, the output for each checkbox is in ",[1,3]" format

if(this.getField("print1").isBoxChecked(0) == "1"){

event.value += this.getField("print1").value;

}

if(this.getField("print2").isBoxChecked(0) == "1"){

event.value += this.getField("print2").value;

}

if(this.getField("print3").isBoxChecked(0) == "1"){

event.value += this.getField("print3").value;

}

//  etc. etc.

//

//

// Print button

var pp = this.getPrintParams();

pp.printRange=[this.getField("Print_pages").value];

this.print(pp);

Question) Is there anyway to condense the if statement into a for loop in above code until there are no more print checkbox fields?

This topic has been closed for replies.
Correct answer try67

You can use something like this:

var i=1;

var f = this.getField("print1");

while (f!=null) {

    if (f.isBoxChecked(0)){

        event.value += f.value;

    }

    i++;

    f = this.getField("print"+i)

}

1 reply

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
August 2, 2018

You can use something like this:

var i=1;

var f = this.getField("print1");

while (f!=null) {

    if (f.isBoxChecked(0)){

        event.value += f.value;

    }

    i++;

    f = this.getField("print"+i)

}

Participant
August 4, 2018

Works perfectly! Thank you!