Copy link to clipboard
Copied
I am very new to writing scripts in PDF and have so far been able to learn a lot just from reading discussions here in this community so thank you to all those that contribute with questions and answers.
As seen in the attachment, I have a table of checkboxes in my form. It is already set so that only 1 box per column can be checked but I am wondering how to also make it so that the user can only check boxes in the same row. For example, if user selects the first "AddAlt1" CB that has an export value of "Cont1", then in "AddAlt2" CB and "AddAlt3" CB, only the boxes with an export value of "Cont1" can be selected.
Copy link to clipboard
Copied
Yes this is possible.
But to be clear. If no checkboxes are selected, then any checkbox can be selected? any row, any column?
Then only checkboxes in the same row as the first selected checkbox can be selected?
To do this you will need to track the value of the first check box selected. All other selections are then rejected if they don't match the saved value. The saved value can only change if all boxes are first unchecked.
I would also suggest renaming the fields to include a block prefix, like this "block.AddAlt1", "block.AddAlt2".
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
Tracking the selected value is done by saving it and watching for all changes to the checkboxes. Probably the best approach is to use a text field. The calculation event is called anytime any value on a form is changed. So it works for both saving the value and tracking changes. For the example code I'll assume the renaming I suggested.
Here's the script for a custom calculation on a hidden text field.
if(event.source && (event.source.name.split(".").shift() == "Block"))
{// only process field changes from the checkbox block
if(event.source.value == "Off")
{// Checkbox is turning off
if(this.getField("Block").getArray().every(a=>a.value == "Off"))
{// all checks are off so clear saved value
event.value = "";
}
}
else if(event.target.value == "")
{// No previously selected value, save current selection
event.value = event.source.value;
}
else
{// Current value, reject selected if it doesn't match
if(event.target.value != event.source.value)
{// Doesn't match so turn off
// Can't directly change field that is triggering this event, so delay
app.setTimeOut('this.getField("' + event.source.name + '").value = "Off";',10);
}
}
}
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
Yes this is possible.
But to be clear. If no checkboxes are selected, then any checkbox can be selected? any row, any column?
Then only checkboxes in the same row as the first selected checkbox can be selected?
To do this you will need to track the value of the first check box selected. All other selections are then rejected if they don't match the saved value. The saved value can only change if all boxes are first unchecked.
I would also suggest renaming the fields to include a block prefix, like this "block.AddAlt1", "block.AddAlt2".
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
Correct, If no checkboxes are selected, then any checkbox can be selected, any row, any column. Then only checkboxes in the same row as the first selected checkbox can be selected. That is correct.
Could you point me in the right direction as to how to track the value and then make the others rejected if they don't match the saved value. Renaming the fields would not be a problem.
Copy link to clipboard
Copied
Tracking the selected value is done by saving it and watching for all changes to the checkboxes. Probably the best approach is to use a text field. The calculation event is called anytime any value on a form is changed. So it works for both saving the value and tracking changes. For the example code I'll assume the renaming I suggested.
Here's the script for a custom calculation on a hidden text field.
if(event.source && (event.source.name.split(".").shift() == "Block"))
{// only process field changes from the checkbox block
if(event.source.value == "Off")
{// Checkbox is turning off
if(this.getField("Block").getArray().every(a=>a.value == "Off"))
{// all checks are off so clear saved value
event.value = "";
}
}
else if(event.target.value == "")
{// No previously selected value, save current selection
event.value = event.source.value;
}
else
{// Current value, reject selected if it doesn't match
if(event.target.value != event.source.value)
{// Doesn't match so turn off
// Can't directly change field that is triggering this event, so delay
app.setTimeOut('this.getField("' + event.source.name + '").value = "Off";',10);
}
}
}
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
Thank you so much! works perfectly.

