Skip to main content
Participating Frequently
February 24, 2020
Answered

Need javascript to checkbox in PDF form based upon value not null in another text field

  • February 24, 2020
  • 4 replies
  • 21421 views

Please help with javscript in text field custom calculation field in Adobe Acrobat Pro DC fillable form.

I have a NO and YES checkbox, with a TextField that will be NULL or have a text value.

  1. I want a check in the checkbox NO, if the text field is NULL
  2. I want a check in the checkbox YES, if the text field is NOT NULL

 

Thanks in Advance!

 

I saw this script on a user board below, but receive a syntax error at line 7, but don't know how to fix.

if((String(event.value) == "") || (event.value == ""))
{
this.getField("LB8No").value = "Off";
this.getField("LB8Yes").value = "Off";
}
else if(event.value <> "")
{
this.getField("LB8No").value = "Off";
this.getField("LB8Yes").value = "On";
}

This topic has been closed for replies.
Correct answer JR Boulay

if (event.value.toString().length > 0)

{this.getField("CHECKBOX").value = "yes";}
else

{this.getField("CHECKBOX").value = "no";}

 

Be sure that the 2 checkboxes have exactly the same name and a different export value ("yes" and "no" in this sample).

Beware that JavaScript is case sensitive ("Yes" is not "yes").

4 replies

JR Boulay
Community Expert
Community Expert
August 18, 2020

"If a checkbox is unchecked, it doesn't get sent, so setting it's value to 0 if it isn't checked isn't going to help - it will always return NULL."

If a checkbox or a group of radio-buttons is not checked the returned value is "Off".

"null" means that the targeted form field does not exist.

Acrobate du PDF, InDesigner et Photoshopographe
JR Boulay
Community Expert
Community Expert
August 18, 2020

"However, it does not automatically check the boxes when the "textfield" is auto-populated with a data function from the datadase that pre-populates the text field."

In this case you should place the script as a "Validation" script instead of a "Calculation" script.

Acrobate du PDF, InDesigner et Photoshopographe
KrunalLathiya
Participant
August 17, 2020

To loosely check if the variable is null, use a double equality operator(==). The double equality operator can not tell the difference between null and undefined, so it counts as same. So if you want to strict check, then don't use a double equality operator. It will lead to misconclusion.

The best way to check for a “null” value in JavaScript is to do a strict equality comparison of the value against the null keyword or Object.is() method and pass both values.

To check null in JavaScript, use triple equals operator(===) or Object.is() method. To find the difference between null and undefined, use the triple equality operator or Object.is() method.

If a checkbox is unchecked, it doesn't get sent, so setting it's value to 0 if it isn't checked isn't going to help - it will always return NULL.

There are two ways to fix this easily:

1) Assume a NULL in the PHP params means the checkbox is unchecked. If the checkbox doesn't always exist on the page, this could be problematic. By the sounds of it, there is a variable number of checkboxes, so this probably won't work.

2) Add a hidden input that has the same name as the checkbox with the value of 0 BEFORE the checkbox. If the checkbox is unchecked, the hidden field value will be used, if it is checked the checkbox value will be used.

 

JR Boulay
Community Expert
JR BoulayCommunity ExpertCorrect answer
Community Expert
February 24, 2020

if (event.value.toString().length > 0)

{this.getField("CHECKBOX").value = "yes";}
else

{this.getField("CHECKBOX").value = "no";}

 

Be sure that the 2 checkboxes have exactly the same name and a different export value ("yes" and "no" in this sample).

Beware that JavaScript is case sensitive ("Yes" is not "yes").

Acrobate du PDF, InDesigner et Photoshopographe
ds_mstarAuthor
Participating Frequently
February 24, 2020

@JR_Boulay - Thanks for your prompt reply! I'll try and confirm on this post if it worked out.