Skip to main content
Known Participant
December 30, 2020
Question

JavaScript for Form Checkboxes

  • December 30, 2020
  • 1 reply
  • 959 views

I am looking to display some type of confirmation message or action when 2 or more checkboxes in a form have been checked. Each form will be different. For example. 

Checkboxes names:

Checkbox1; Checkbox2; Checkbox3; Checkbox4.

I want to display message or initiate an action like going to a page when Checkbox1 AND 2 AND 3 are selected. 

Thank You

This topic has been closed for replies.

1 reply

try67
Community Expert
Community Expert
December 30, 2020

Create a (hidden) text field with the following code as its custom calculation script:

 

var fields = ["Checkbox1", "Checkbox2", "Checkbox3", "Checkbox4"];
if (event.source!=null && fields.indexOf(event.source.name)!=-1) {
	var counter = 0;
	for (var i in fields) {
		var f = this.getField(fields[i]);
		if (f.valueAsString!="Off") counter++;
	}
	if (counter>=2) {
		app.alert("More then two check-boxes are ticked.",3); // or do something else
	}
}
Known Participant
December 30, 2020

Hi Try67. Thanks for the reply. I was vague in my initial post and have been playing around a bit and got it to work using the 'App Alert'. I would like a button to initiate a check and to have the pop up something other then a Java Warning Window. Maybe Display a Text Field or an Image or even open a page within the PDF? I think the easiest would to show the Text Field? The number of check boxes may vary. Sometimes 1...Sometimes more than 1. Big picture I would like to modify that 'App Alert' command to a show field command or something equivalent?

My Button Script Currently:

if (this.getField("CheckBox1").value=="Yes" || this.getField("CheckBox2").value=="Yes" || this.getField("CheckBox3").value=="Yes")
{app.alert("You have isolated the component.");} else {app.alert("You have not isolated the component.");}

try67
Community Expert
Community Expert
December 31, 2020

To show a field use this:

this.getField("FieldName").display = display.visible;

To make it hidden use this:

this.getField("FieldName").display = display.hidden;