• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Conditionnal check box

Explorer ,
Sep 23, 2020 Sep 23, 2020

Copy link to clipboard

Copied

I'm new to JS. I know it should be easy to do that but I didn't find my answer anywhere.

I have a check box that I want to be checked only if there is a value in either one of my three drop-down list. 

Every script I tried didn't work out. 

How should I write it ?

 

If Drop1 or Drop 2 or Drop 3 is not empty, check Box 1

Thanks again, 

TOPICS
Acrobat SDK and JavaScript

Views

2.2K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Sep 23, 2020 Sep 23, 2020

Hi, sorry guys for meddling, if I got it right you want to check checkbox if values are not empty in one of 3 dropdown fields
and to be unchecked if no items are selected in dropdowns?
If thats all you want, add empty value to each dropdown and give it export value of 0,
then you can use this code as custom calculation script of one of dropdown fields or another hidden text field:

if(this.getField("Decoupe").value != 0){
this.getField("Dessin").checkThisBox(0, true);}
else if(this.getField("Assemblage

...

Votes

Translate

Translate
Community Expert ,
Sep 23, 2020 Sep 23, 2020

Copy link to clipboard

Copied

I assume that the checkbox should never be checked or unchecked by the user directly, so make it read-only. 

 

Now the problem is that a checkbox does not have a calculation script associoated with it, so the best way to do that (at least in my opinion), is to have another field in your form that is hidden and read-only. This way, it will not show up on the form, and the user cannot accidentially tab into it. I use a text field for that, and I usually name it "hiddenCalculation" to indicate what it's purpose is. When you then use the following script (after modifying the field names to match your form) as the calculation script in the hidden field, it should work:

 

var d1 = this.getField("Dropdown1").value;
var d2 = this.getField("Dropdown2").value;
var d3 = this.getField("Dropdown3").value;

var cb = this.getField("Checkbox");

cb.value = (d1 || d2 || d3) ? "Yes" : "Off";

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 23, 2020 Sep 23, 2020

Copy link to clipboard

Copied

Thanks for this. 

The issue is that the check box could be check by the user too...

I have a form that will be filled by the customer to order some products. 

There is options (the three drop downs) that can have a value or not.

If the client chose one of the option, he must send a drawing with his form. That's why the box should be checked if something is selected in one of the drop downs.

But, he can choose to send a drawing even if none of theese options are checked. 

If you have another solution for me I can do it differently. 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Sep 23, 2020 Sep 23, 2020

Copy link to clipboard

Copied

Don't make it read only. Also how did you make so dropdown doesn't have value?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 23, 2020 Sep 23, 2020

Copy link to clipboard

Copied

Making the field writable will not work becaue every time something changes in the form, the value woud be calculated again, and the user's input would be repalced with the calculated value. You can select to not have a default value by not having any dropdown value highlighted when you create the list of values. You need to click into the empty space of the list to toggle the default selection. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 23, 2020 Sep 23, 2020

Copy link to clipboard

Copied

I would add a second checkbox: The first one is "attached drawing is required" and the second one is "attached drawing without being required". It is very hard to come up with a mechanism to allow both automatic changes and manual changes. You can find the reason why the calculation event was triggered by inspecting the event.source property, and you can then see if a change should override the current setting or not. You would also have to keep track on if the user checked the box without the form requiring that checkmark. You would do that in a hidden field. Then you need to come up with a set of rules that describe exactly what the behavior should be in all possible combinations of user interacions with the form. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 23, 2020 Sep 23, 2020

Copy link to clipboard

Copied

Thanks for the explanations. It seems a little complex to me. 

What if a simply add a pop-up alert when the customer click on either Send or Print buttons (these are buttons I made, not the application buttons) ?

I'd like to have something like that :

If there is at least one of the three drop down filled and the check box is empty, the message pops. 

"The check box Dessin is unselected but you need to provide us a drawing since you have options in fields A B or C. "

How could I do that ? 

Thanks again, 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 23, 2020 Sep 23, 2020

Copy link to clipboard

Copied

Take a look at the app.alert() function for displaying a message (https://help.adobe.com/en_US/acrobat/acrobat_dc_sdk/2015/HTMLHelp/index.html#t=Acro12_MasterBook%2FJ...)

 

You could do somthing like this in your Send or Print click handler: 

 

var d1 = this.getField("Dropdown1").value;
var d2 = this.getField("Dropdown2").value;
var d3 = this.getField("Dropdown3").value;


if (d1 || d2 || d3) {
    app.alert("Dropdown 1, Dropdown 2 or Dropdown 3 have a selection");
}

 

For printing, you could even hook into the document's "Will Print" action and check there so that you don't have to do this in your own print button, but it would also work when the user uses the application's print function. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 23, 2020 Sep 23, 2020

Copy link to clipboard

Copied

I tried it. So I created an hidden field. Marked it as read only.

I also marked the check box as read only.

I copied the code and remplaced the values. It doesn't work. What can be wrong ? I put the script in the personnalized calculation section of the hidden field.

 

var d1 = this.getField("Decoupe").value;
var d2 = this.getField("Assemblagedessus").value;
var d3 = this.getField("Assemblagepattes").value;

var cb = this.getField("Dessin");

cb.value = (d1 || d2 || d3) ? "Yes" : "Off";

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 23, 2020 Sep 23, 2020

Copy link to clipboard

Copied

Are you getting any errors in the JavaScript console? You can bring up the console using Ctrl-J on Windows, or Cmd-J on a Mac. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 23, 2020 Sep 23, 2020

Copy link to clipboard

Copied

This line won't work as intended:

 

cb.value = (d1 || d2 || d3) ? "Yes" : "Off";

 

Since d1, d2, and d3 will always evalutate to True, unless the user managed to set up a zero-length items, which is rare. I'll let you fix it: ;^)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 23, 2020 Sep 23, 2020

Copy link to clipboard

Copied

It does work if the dropdowns don't have a default value selected. I have a form here that uses exactly this line, and it works 🙂 

 

The problem may be that I am making assumptions that are not true... More about that further down. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 23, 2020 Sep 23, 2020

Copy link to clipboard

Copied

Karl,

 

How are you creating a dropdown where there isn't anything selected? That is, when you get the field value and it's a zero length string. I know of one way, but it isn't straightforward.

 

Thanks.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 23, 2020 Sep 23, 2020

Copy link to clipboard

Copied

When you add items to the list, the last item you've added will always be highlighted:

 

2020-09-23_14-36-54.png

 

As the bottom of the dialog explains, the highligted item will be the default choice. You cannot unselect something by clicking on the selected item (which would be the obvious choice to toggle a selection), but you can deselect the selected item by clicking below the list:

 

2020-09-23_14-37-19.png

The screenshot was taken right after I clicked below the last list item. As you can see, the highlight is gone, and when you now close the dialog, the default will be no selection. This state is restored when you reset the form after making a selection. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 23, 2020 Sep 23, 2020

Copy link to clipboard

Copied

Wow, that's crazy. I've been doing this for a while now and that's the first I've heard of that. I can say that space at the bottom is much less on Windows than what your screen shot shows, so that's probably why I've never noticed. Thanks!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 23, 2020 Sep 23, 2020

Copy link to clipboard

Copied

I have no errors in the console. When doing CTRL+Enter I have Yes in return.

var d1 = this.getField("Decoupe").value;
var d2 = this.getField("Assemblagedessus").value;
var d3 = this.getField("Assemblagepattes").value;

var cb = this.getField("Dessin");

cb.value = (d1 || d2 || d3) ? "Yes" : "Off";
Yes

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 23, 2020 Sep 23, 2020

Copy link to clipboard

Copied

Let's take a look at what your dropdown controls look like. To see what value they export when you don't have anything selected, we can have Acrobat display these values in the console. To do that, add the following to your code (this needs to be  after the calculation script from above in your hidden field):

 

 

console.println("Value of Dropown 1: " + d1.toSource());
console.println("Value of Dropown 2: " + d2.toSource());
console.println("Value of Dropown 3: " + d3.toSource());

 

 

Then change your dropdowns a few times and post what you find in the JavaScript console here. 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 23, 2020 Sep 23, 2020

Copy link to clipboard

Copied

Ok it is a little hard for me to understand. I never used JS before and I never created a form before. Maybe there is something wrong in the way I build it. My form has 3 Items sections with the same fields but I rename them with a number at the end of the name. I also have a button that spawn new pages to have more items. In my template page, I have 3 more items. 

Here is what I had in the console. even though I probably didn't do it correctly.

 

TypeError: this.getField(...) is null
1:AcroForm:Typedecollage2:Calculate
TypeError: this.getField(...) is null
1:AcroForm:Typedecollage3:Calculate
TypeError: this.getField(...) is null
1:AcroForm:Typedecollage5:Calculate
TypeError: this.getField(...) is null
1:AcroForm:Typedecollage5:Calculate
TypeError: this.getField(...) is null
1:AcroForm:Typedecollage4:Calculate
TypeError: this.getField(...) is null
1:AcroForm:Typedecollage:CalculateValue of Dropown 1: (new String("Trou non-poli (topmount)"))
Value of Dropown 2: (new String(""))
Value of Dropown 3: (new String(""))

 

Then when I cleared the value with my button :

1:AcroForm:Typedecollage:CalculateValue of Dropown 1: (new String(" "))
Value of Dropown 2: (new String(""))
Value of Dropown 3: (new String(""))

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 23, 2020 Sep 23, 2020

Copy link to clipboard

Copied

The last three lines show me that your first dropdown uses one space as the "blank" element, you've very likely added that to the list of options. The other two show a true empty string, which is what my script expects. The error messages indicate that there is a problem with one or more of your field names. Would you be able to share the form you are working on? If you are not able to share this publicly, you can also share it with me directly. My contact information should be in my profile. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 23, 2020 Sep 23, 2020

Copy link to clipboard

Copied

Yes I could share my file, but I don't see any options to attach a file even in the private message section.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 23, 2020 Sep 23, 2020

Copy link to clipboard

Copied

You would have to upload it to a file sharing service (e.g. Adobe's own Document Cloud or Dropbox) and then share the link here. 

 

I created a sample documet that does what you want to do. You can download it here: 

https://khkonsulting.com/files/AUC/checkbox.pdf

 

I did change the blank entry to just one space for all three dropdown controls. This allows you to switch back and forth between a valid entry and a blank entry. If you use just the default blank selection, then there is no way to switch back to the original state of the form. This does change the way we are testing for an all blank configuration. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 23, 2020 Sep 23, 2020

Copy link to clipboard

Copied

Wow ! Thank you so much. I think I'll try to reproduce what you did in the sample and if it doesn't work i'll share my file. 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 19, 2020 Oct 19, 2020

Copy link to clipboard

Copied

Great tip Karl.  I had a text field that has a value entered from a checkbox.  The text field already has a custom calculation, so I don't think you can add a second one to the same text field.  Your hidden field suggestion and script did the trick!  My rudimentary Javascript knowledge allowed me to change your script to match my document.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 19, 2020 Oct 19, 2020

Copy link to clipboard

Copied

LATEST

That depends what you are calculating, you could probably add calculation to existing calculation, adding extra field and extra calculations can slow down your file. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 23, 2020 Sep 23, 2020

Copy link to clipboard

Copied

Hi, sorry guys for meddling, if I got it right you want to check checkbox if values are not empty in one of 3 dropdown fields
and to be unchecked if no items are selected in dropdowns?
If thats all you want, add empty value to each dropdown and give it export value of 0,
then you can use this code as custom calculation script of one of dropdown fields or another hidden text field:

if(this.getField("Decoupe").value != 0){
this.getField("Dessin").checkThisBox(0, true);}
else if(this.getField("Assemblagedessus").value != 0){
this.getField("Dessin").checkThisBox(0, true);}
else if(this.getField("Assemblagepattes").value != 0){
this.getField("Dessin").checkThisBox(0, true);}
else this.getField("Dessin").checkThisBox(0, false);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines