Highlighted

Select/deselect a checkbox as related checkboxes are selected/deselected

New Here ,
Sep 22, 2020

Copy link to clipboard

Copied

I'm creating a PDF form using Acrobat DC. The form has five checkboxes for tests available from Laboratory A and 18 checkboxes for tests available from Laboratory B. I'm looking for a way that I can have a checkbox for Laboratory A (LabA_CB) automatically select when one or more of the available tests at Laboratory A is selected, and a second checkbox for Laboratory B (LabB_CB) automatically select when one or more of the available tests at Laboratory B is selected.

However, I also need the Laboratory A and B checkboxes to deselect if all of the associated tests for that lab are deselected. The code I've tried so far either does not deselect the Laboratory A and B checkboxes even if all associated test checkboxes are deselected, or it deselects the Laboratory A and B checkboxes each time one of the associated tests is deselected even if other associated tests remain selected. I think this is because the only code I've been able to find is for the mouse up event for each of the individual test checkboxes.

I am a complete JavaScript novice, but I hope there is a way to make the Laboratory A and B checkboxes "check" to see if any of the tests associated with that lab have been selected/deselected. Each of my test checkboxes is named with the associated lab and actual test name (e.g., LabA_Protein_CB, LabA_Fiber_CB, LabB_Iron_CB, LabB_Lead_CB) and the export value is also set to the test name. I need it to be set up this way for exporting data into Excel.

Any advice would be much appreciated! Searching for anything related to checkboxes seems to only return answers regarding select/deselect all or calculations.

Adobe Community Professional
Correct answer by George_Johnson | Adobe Community Professional

OK, for the Lab A text field, you can use a custom calculate script like this:

 

// Custom calculate script
(function () {

    // Get a reference to the LabA_CB field
    var f_labA = getField("LabA_CB");

    // Create an array of check box names to check
    var aFields = [
        "LabA_Proximate1_CB",
        "LabA_Proximate2_CB",
        "LabA_NDF_CB",
        "LabA_Other1_CB",
        "LabA_Other2_CB"
    ];

    // Scan through the check boxes to see if any are selected
    for (var i = 0; i < aFields.length; i += 1) {

        if (getField(aFields[i]).value !== "Off") {

            // Select the check box by setting its value to its export value
            // You could also use the checkThisBox method instead, but I never liked it
            f_labA.value = "Yes";  // replace "Yes" with the actual export value

            // No need to scan further since we found a selected check box
            return;
        }
    }

    // None are selected, so deselect the check box
    f_labA.value = "Off";

})();

 

Be sure to remove any Mouse Up scripts you've added.

TOPICS
Acrobat SDK and JavaScript, PDF forms

Views

72

Likes

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

Select/deselect a checkbox as related checkboxes are selected/deselected

New Here ,
Sep 22, 2020

Copy link to clipboard

Copied

I'm creating a PDF form using Acrobat DC. The form has five checkboxes for tests available from Laboratory A and 18 checkboxes for tests available from Laboratory B. I'm looking for a way that I can have a checkbox for Laboratory A (LabA_CB) automatically select when one or more of the available tests at Laboratory A is selected, and a second checkbox for Laboratory B (LabB_CB) automatically select when one or more of the available tests at Laboratory B is selected.

However, I also need the Laboratory A and B checkboxes to deselect if all of the associated tests for that lab are deselected. The code I've tried so far either does not deselect the Laboratory A and B checkboxes even if all associated test checkboxes are deselected, or it deselects the Laboratory A and B checkboxes each time one of the associated tests is deselected even if other associated tests remain selected. I think this is because the only code I've been able to find is for the mouse up event for each of the individual test checkboxes.

I am a complete JavaScript novice, but I hope there is a way to make the Laboratory A and B checkboxes "check" to see if any of the tests associated with that lab have been selected/deselected. Each of my test checkboxes is named with the associated lab and actual test name (e.g., LabA_Protein_CB, LabA_Fiber_CB, LabB_Iron_CB, LabB_Lead_CB) and the export value is also set to the test name. I need it to be set up this way for exporting data into Excel.

Any advice would be much appreciated! Searching for anything related to checkboxes seems to only return answers regarding select/deselect all or calculations.

Adobe Community Professional
Correct answer by George_Johnson | Adobe Community Professional

OK, for the Lab A text field, you can use a custom calculate script like this:

 

// Custom calculate script
(function () {

    // Get a reference to the LabA_CB field
    var f_labA = getField("LabA_CB");

    // Create an array of check box names to check
    var aFields = [
        "LabA_Proximate1_CB",
        "LabA_Proximate2_CB",
        "LabA_NDF_CB",
        "LabA_Other1_CB",
        "LabA_Other2_CB"
    ];

    // Scan through the check boxes to see if any are selected
    for (var i = 0; i < aFields.length; i += 1) {

        if (getField(aFields[i]).value !== "Off") {

            // Select the check box by setting its value to its export value
            // You could also use the checkThisBox method instead, but I never liked it
            f_labA.value = "Yes";  // replace "Yes" with the actual export value

            // No need to scan further since we found a selected check box
            return;
        }
    }

    // None are selected, so deselect the check box
    f_labA.value = "Off";

})();

 

Be sure to remove any Mouse Up scripts you've added.

TOPICS
Acrobat SDK and JavaScript, PDF forms

Views

73

Likes

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
Sep 22, 2020 0
Adobe Community Professional ,
Sep 22, 2020

Copy link to clipboard

Copied

A common way to deal with this is to set up a hidden text field and set up a calculation script with it that does the job of the automatic selecting/deselecting of the checkboxes. The text field's calculate script will be triggered whenever any of the checkboxes is manually changed.

 

So the code could first scan through the relevant Lab A fields to see if any have been selected, and set the value of the LabA_CB to either "Off" (to deselect) or whatever you set the export value to (to select). Same idea for the Lab B fields.

 

This simplifies things since you'll only need a single calculation script for the hidden text field, and not multiple Mouse Up scripts for each check box. Post again if you need help with the code, and include what you've tried.

Likes

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
Reply
Loading...
Sep 22, 2020 0
New Here ,
Sep 22, 2020

Copy link to clipboard

Copied

That sounds like exactly what I need - I just don't know how to write the code to accomplish it. 

 

I tried this as the Mouse Up script for each of the test checkboxes associated with Lab A, but it doesn't deselect the LabA_CB when tests are deselected:

if (event.target.value!="Off") this.getField("LabA_CB").checkThisBox(0, true);

 

I also got desperate enough to try resetting all of the export values for the test checkboxes to "Lab A" and then using this as the Mouse Up script (this is where I ran into the issue with it deselecting LabA_CB when deselecting any Lab A test):

this.getField ("Lab A").value = event.target.value;

 

So I created two new text fields in my form (LabA_Calc_TB and LabB_Calc_TB) and set them to hidden. What do I need to enter into each of the custom calculation scripts to make it "scan through" the tests associated with each lab? For the sake of example, here are the field names for the test checkboxes associated with Lab A: 

LabA_Proximate1_CB

LabA_Proximate2_CB

LabA_NDF_CB

LabA_Other1_CB

LabA_Other2_CB

Likes

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
Reply
Loading...
Sep 22, 2020 0
Adobe Community Professional ,
Sep 22, 2020

Copy link to clipboard

Copied

OK, for the Lab A text field, you can use a custom calculate script like this:

 

// Custom calculate script
(function () {

    // Get a reference to the LabA_CB field
    var f_labA = getField("LabA_CB");

    // Create an array of check box names to check
    var aFields = [
        "LabA_Proximate1_CB",
        "LabA_Proximate2_CB",
        "LabA_NDF_CB",
        "LabA_Other1_CB",
        "LabA_Other2_CB"
    ];

    // Scan through the check boxes to see if any are selected
    for (var i = 0; i < aFields.length; i += 1) {

        if (getField(aFields[i]).value !== "Off") {

            // Select the check box by setting its value to its export value
            // You could also use the checkThisBox method instead, but I never liked it
            f_labA.value = "Yes";  // replace "Yes" with the actual export value

            // No need to scan further since we found a selected check box
            return;
        }
    }

    // None are selected, so deselect the check box
    f_labA.value = "Off";

})();

 

Be sure to remove any Mouse Up scripts you've added.

Likes

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
Reply
Loading...
Sep 22, 2020 0
New Here ,
Sep 22, 2020

Copy link to clipboard

Copied

IT WORKED! I was able to add the script for Lab B as well based on what you provided and it works like a charm. Kicking myself for not posting sooner. Thank you!!

Likes

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
Reply
Loading...
Sep 22, 2020 0