Skip to main content
Glenn269
Known Participant
December 17, 2020
Answered

Would like option of one checkbox that would automatically check multiple checkboxes

  • December 17, 2020
  • 2 replies
  • 850 views

Hi there,

I have multiple checkboxes (named "CheckBox1", "Checkbox2", and "CheckBox3")

 

I would like it so that the end-user can individually check these checkboxes and they have the option to click a separate checkbox (let's name it "CheckBoxAll") so that all of the checkboxes are checked and the CheckBox1, 2, and 3 are also locked.  If the CheckBoxAll is then unchecked, it will revert back to all boxes being unchecked.

 

I'm guessing I would want a Mouse Up JavaScript in the CheckBoxAll checkbox, but I can't figure out what to put in there.

 

Any ideas?  Thanks in advance.
Glenn

This topic has been closed for replies.
Correct answer Nesa Nurani

Yes, use MouseUp in CheckBoxAll:

if(event.target.isBoxChecked(0)){
this.getField("Checkbox1").checkThisBox(0,true);
this.getField("Checkbox1").readonly = true;
this.getField("Checkbox2").checkThisBox(0,true);
this.getField("Checkbox2").readonly = true;
this.getField("Checkbox3").checkThisBox(0,true);
this.getField("Checkbox3").readonly = true;}
else{
this.getField("Checkbox1").checkThisBox(0,false);
this.getField("Checkbox1").readonly = false;
this.getField("Checkbox2").checkThisBox(0,false);
this.getField("Checkbox2").readonly = false;
this.getField("Checkbox3").checkThisBox(0,false);
this.getField("Checkbox3").readonly = false;}

2 replies

Nesa Nurani
Nesa NuraniCorrect answer
Community Expert
December 17, 2020

Yes, use MouseUp in CheckBoxAll:

if(event.target.isBoxChecked(0)){
this.getField("Checkbox1").checkThisBox(0,true);
this.getField("Checkbox1").readonly = true;
this.getField("Checkbox2").checkThisBox(0,true);
this.getField("Checkbox2").readonly = true;
this.getField("Checkbox3").checkThisBox(0,true);
this.getField("Checkbox3").readonly = true;}
else{
this.getField("Checkbox1").checkThisBox(0,false);
this.getField("Checkbox1").readonly = false;
this.getField("Checkbox2").checkThisBox(0,false);
this.getField("Checkbox2").readonly = false;
this.getField("Checkbox3").checkThisBox(0,false);
this.getField("Checkbox3").readonly = false;}
Glenn269
Glenn269Author
Known Participant
December 17, 2020

Yet again to the rescue!  Thank you Nesa.
CheckBoxes 1, 2, and 3 also have their own scripts and I was hoping that when the above CheckBoxAll script ran that the individual scripts in CheckBox1, 2, and 3 would then run too...But they didn't...Probably because I didn't specify they needed to!!!
I tested the following (with the first checkbox) which seemed to work, and I just wondered if by doing this format for the remaining checkboxes is this the best/only way, or is there an easier way for this to work?

 

if(event.target.isBoxChecked(0)){

this.getField("CheckBoxNA7.1").checkThisBox(0,true);

this.getField("CheckBoxNA7.1").readonly = true; }

if(event.target.value != "Off"){

this.getField("Score7.1").value = "";

this.getField("CheckBoxD7.1").value = "Off";

this.getField("CheckBoxD7.1").readonly = true;}

else

this.getField("CheckBoxNA7.1").checkThisBox(0,false);

this.getField("CheckBoxNA7.1").readonly = false;

if(event.target.value == "Off"){

this.getField("Score7.1").value = 3;

this.getField("CheckBoxD7.1").readonly = false;}

Glenn269
Glenn269Author
Known Participant
December 18, 2020

It won't run script automatically,if you set up script in MouseUp event then you need to click on field to activate script.

If you want to run that script when "CheckBoxAll" is clicked then add script to "CheckBoxAll" MouseUp event, or make hidden text field and put script in "Custom calculaton script" so it will execute automatically.


That makes perfect sense.  Thank you.

Inspiring
December 17, 2020

You can set a field to read-only to prevent a user from interacting with it. The code for a single checkbox can be as simple as:

 

getField("Checkbox1").readonly = true;

 

and you can set it to false to make it user selectable.

 

The code can be simplified if you use hierarchcal naming for the group of checkboxes (e.g., group1.cb1, group1.cb2, etc.) and the code to disable all in the group can be:

 

getField("group1").readonly = true;