Skip to main content
Known Participant
January 2, 2018
Answered

Simultaneously check/uncheck two checkboxes with different names-always checked or unchecked together

  • January 2, 2018
  • 2 replies
  • 5226 views

I have a form with two groups of related products and prices. When a user checks one product from the first group, I need it to automatically check the related product from the second group and display their prices if checked. They both need to uncheck together as well.

The checkboxes in the different product groups need different names because I am also showing/hiding related prices based on the checkbox being checked and a variable quantity.

My code doesn't quite work:

The code for the First Checkbox

  1. Displays the Price field when checkbox is checked, and
  2. Checks both boxes (First Checkbox + Matching Checkbox)
  3. but, it won't uncheck both boxes if one or the other is unchecked.

var nHide = event.target.isBoxChecked(0)?display.visible:display.hidden;

this.getField(“Price”).display = nHide;

if (event.target.value!="Off") this.getField(“Matching Checkbox”).checkThisBox(0, true);

Is it possible to get the checkboxes to check and uncheck together, just as if they had the same name?

Thanks for any assistance you can provide!

This topic has been closed for replies.
Correct answer Thom Parker

First, make sure all the check boxes use the same export value. At least all the check boxes that relate to one another should have the same export value.

Then use this code in the Mouse Up script of the parent product.

this.getField ("Matching Checkbox").value = event.target.value;

This will cause all of the related product check boxes to go on and off with the parent.

2 replies

New Participant
June 22, 2020

Is there a way to uncheck all if they don't all have the same Export Value?

New Participant
June 22, 2020

I have 2 javascripts

1. 

var otherCheckBoxes = ["Checkbox1", "Checkbox2", "Checkbox3"]
if (getField("Select All_1").value!="off") {for (var i in otherCheckBoxes) getField(otherCheckBoxes[i]).checkThisBox(0,true);}

2.

var otherCheckBoxes = ["Checkbox1", "Checkbox2", "Checkbox3"]
if (getField("Select All_1").value!="Off") {for (var i in otherCheckBoxes) getField(otherCheckBoxes[i]).checkThisBox(0,false);}

 

But when parent box is checked all child boxes uncheck and when parent box is unchecked all child boxes check.  LOL

Thom Parker
Thom ParkerCorrect answer
Community Expert
January 2, 2018

First, make sure all the check boxes use the same export value. At least all the check boxes that relate to one another should have the same export value.

Then use this code in the Mouse Up script of the parent product.

this.getField ("Matching Checkbox").value = event.target.value;

This will cause all of the related product check boxes to go on and off with the parent.

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Known Participant
January 2, 2018

Thom,

Thanks for the reply. Your solution worked perfectly, but I see there's more to it than I first realized. (I am a rookie at Javavscript)

See my example. When I check/uncheck either Checkbox 1, they both check/uncheck in tandem as expected, and Extended Price 1 is hidden if unchecked, but Extended price 4 is still visible.

What is missing is I want to show/hide Extended price 1 AND Extended price 4 with the state of the checkboxes—Check=show/Uncheck=hide (and, same with the extended prices related to Checkboxes 2, 3, etc.)

Check/UncheckShow/Hide both group prices

Checkbox 1 (Group 1)

javascript:;

Extended price 1

Checkbox 2 (Group 1)

Extended price 2

Checkbox 3 (Group 1)

Extended price 3

Checkbox 1 (Group 2)

Extended price 4

Checkbox 2 (Group 2)

Extended price 5

Checkbox 3 (Group 2)

Extended price 6

Is that doable?

Thank you so much! And happy new year!

Thom Parker
Community Expert
January 2, 2018

It is all doable. So if I understand correctly, Price1 is hidden/shown with the checkbox1 in Group 1, Price 2 with Checkbox2 in group1, etc, price4 with checkbox1 in group2, price 5 with checkbox2 in group 2.

If this is correct, then price 1 and price 4 are best controlled from the mouseUp scripts on the checkbox 1 fields. Your original script will work for this. It just needs to be modified to hide/show the correct price field.

But you have a complication because the value of the child check boxes can come from 2 different sources, the parent and the child, so you need to repeat the show/hide code in both MouseUps.

For example, for goup 1, use this code in the Checkbox1 MouseUp

this.getField("Price1").display = (event.target.value=="Yes")?display.visible:display.hidden;

this.getField("Price2").display = (event.target.value=="Yes")?display.visible:display.hidden;

this.getField("Price3").display = (event.target.value=="Yes")?display.visible:display.hidden;

Then for the checkbox 2 add this code to the Mouse up

this.getField("Price2").display = (event.target.value=="Yes")?display.visible:display.hidden;

This is not an ideal or clean solution from a programming point of view, but it is straight forward and will work.

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