Skip to main content
Known Participant
June 3, 2016
Answered

Checkbox, select and disable.

  • June 3, 2016
  • 1 reply
  • 864 views

I have code that when a specific checkbox is checked, several other checkboxes become checked. I need ONLY those other checkboxes that get Checked to now be greyedout/locked checked. So the only way to uncheck them would be to deselect the original checkbox. The code is below.....

var otherCheckBoxes = ["Local Travel", "Taxi", "Public Transit", "Province", "Region", "Private Motor Vehicle", "Government Fleet vehicle",  "Air1", "Rail1", "Bus1", "Ferry1", "Taxi3", "Rental Motor Vehicle1", "Public Transit1", "Private Motor Vehicle2",  "Government Fleet vehicle2", "Air", "Rail", "Bus", "Ferry", "Taxi2", "Rental Motor Vehicle", "Public Transit2", "Private Motor Vehicle1",  "Government Fleet vehicle1"]; // etc.

if (getField("Canada").value!="Off") {

for (var i in otherCheckBoxes) getField(otherCheckBoxes).checkThisBox(0,true);

}

else if  (getField("Canada").value!="On")  {

for (var i in otherCheckBoxes) getField(otherCheckBoxes).checkThisBox(0,false);

}

This topic has been closed for replies.
Correct answer try67

Change the last part of the code to:

if (this.getField("Canada").value!="Off") {

    for (var i in otherCheckBoxes) {

        this.getField(otherCheckBoxes).checkThisBox(0,true);

        this.getField(otherCheckBoxes).readonly = true;

    }

} else if (this.getField("Canada").value!="On")  {

    for (var i in otherCheckBoxes) {

        this.getField(otherCheckBoxes).checkThisBox(0,false);

        this.getField(otherCheckBoxes).readonly = false;

    }

}

1 reply

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
June 3, 2016

Change the last part of the code to:

if (this.getField("Canada").value!="Off") {

    for (var i in otherCheckBoxes) {

        this.getField(otherCheckBoxes).checkThisBox(0,true);

        this.getField(otherCheckBoxes).readonly = true;

    }

} else if (this.getField("Canada").value!="On")  {

    for (var i in otherCheckBoxes) {

        this.getField(otherCheckBoxes).checkThisBox(0,false);

        this.getField(otherCheckBoxes).readonly = false;

    }

}

try67
Community Expert
Community Expert
June 3, 2016

By the way, your code is a bit confusing. Why use the NOT-EQUALS operator instead of the EQUALS one? It would make it more readable.

It can even be simplified even more, to:

var isCanada = (this.getField("Canada").value!="Off");

for (var i in otherCheckBoxes) {

    this.getField(otherCheckBoxes).checkThisBox(0, isCanada);

    this.getField(otherCheckBoxes).readonly = isCanada;

}