Skip to main content
Known Participant
October 5, 2023
Question

If and Else Statements in Adobe Forms

  • October 5, 2023
  • 2 replies
  • 1039 views

I have five Checkboxes if I check checkbox 1 and 4, Checkbox 5 is checked. If I check checkbox 2 and 4, Checkbox 5 is checked. If I check Checkbox 3 and Checkbox 4, then Checkbox 5 is checked

 

I entered the following code Javascript code in the Javascript Tool (Document Javascript strings) in Acrobat, saved it but it is not working. Any help would be creatly appreciated.

 

// Function to update Checkbox 5 based on conditions
function updateCheckbox5() {
var checkbox1 = this.getField("Checkbox1").value;
var checkbox2 = this.getField("Checkbox2").value;
var checkbox3 = this.getField("Checkbox3").value;
var checkbox4 = this.getField("Checkbox4").value;
var checkbox5 = this.getField("Checkbox5");

if ((checkbox1 === "On" && checkbox4 === "On") ||
(checkbox2 === "On" && checkbox4 === "On") ||
(checkbox3 === "On" && checkbox4 === "On")) {
checkbox5.value = "On";
} else {
checkbox5.value = "Off";
}
}

// Add event listeners to the checkboxes
this.getField("Checkbox1").addEventListener("MouseUp", updateCheckbox5);
this.getField("Checkbox2").addEventListener("MouseUp", updateCheckbox5);
this.getField("Checkbox3").addEventListener("MouseUp", updateCheckbox5);
this.getField("Checkbox4").addEventListener("MouseUp", updateCheckbox5);

This topic has been closed for replies.

2 replies

Bernd Alheit
Community Expert
Community Expert
October 6, 2023

Check the JavaScript console for errors.

Thom Parker
Community Expert
Community Expert
October 5, 2023

First, there's no such thing as an EventListerner in the Acrobat JavaScript model. You enter the code manually in the event script area.

Read this:

https://www.pdfscripting.com/public/Editing-Fields-Properties.cfm?sd=40

 

Next, the "updateCheckbox5" function should be a document level script. Then all that needs to be entred into the individual checkbox MouseUp scripts is the function call. 

 

Last, there is at least one error in your code.  The first place to look for reported errors is the JavaScript Console.

Watch this tutorial:

https://www.pdfscripting.com/public/images/video/AcroJSIntro/AcroJSIntro_ConsoleWindow.cfm

 

  

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Known Participant
October 5, 2023

Hi Thom, first of all thanks for the condescending answer. I have not been here long but am not interested in receiving answers that point me to your PDF scripting site, that for the most part seems to be a pay site to receive any useful information. Just a guy new to JavaScript asking for help. Have a great weekend.

Thom Parker
Community Expert
Community Expert
October 6, 2023

I apologize for pointing out the errors and providing solutions.

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often