diff --git a/DigitalLearningSolutions.Web/Scripts/frameworks/selectoptionalcompetencies.ts b/DigitalLearningSolutions.Web/Scripts/frameworks/selectoptionalcompetencies.ts index 0417a0b34a..2cd7a44579 100644 --- a/DigitalLearningSolutions.Web/Scripts/frameworks/selectoptionalcompetencies.ts +++ b/DigitalLearningSolutions.Web/Scripts/frameworks/selectoptionalcompetencies.ts @@ -2,26 +2,31 @@ const groups = document.querySelectorAll('.nhsuk-checkboxes'); groups.forEach((group) => { - const groupToggle = group.querySelector('input[name="GroupIds"]'); - if (!groupToggle) return; - + const groupToggle = + group.querySelector('input[name="GroupIds"]'); // All individual competency checkboxes in the group - const childCheckboxes = group.querySelectorAll( - 'input[name="SelectedCompetencyIds"]', - ); + const childCheckboxes = + group.querySelectorAll( + 'input[name="SelectedCompetencyIds"]', + ); - const updateState = () => { - const isChecked = groupToggle.checked; + if (!groupToggle) { + return; + } - childCheckboxes.forEach((_, index) => { - childCheckboxes[index].checked = isChecked; + const setChildrenCheckedState = (checked: boolean) => { + childCheckboxes.forEach((checkboxElement) => { + const checkbox = checkboxElement; + checkbox.checked = checked; }); }; - // Run when the group checkbox changes - groupToggle.addEventListener('change', updateState); + if (groupToggle.checked) { + setChildrenCheckedState(true); + } - // Also run at page load in case some are pre-checked server-side - updateState(); + groupToggle.addEventListener('change', () => { + setChildrenCheckedState(groupToggle.checked); + }); }); }); diff --git a/DigitalLearningSolutions.Web/Views/CompetencyAssessments/SelectOptionalCompetencies.cshtml b/DigitalLearningSolutions.Web/Views/CompetencyAssessments/SelectOptionalCompetencies.cshtml index cb3df401cf..13bfd420f7 100644 --- a/DigitalLearningSolutions.Web/Views/CompetencyAssessments/SelectOptionalCompetencies.cshtml +++ b/DigitalLearningSolutions.Web/Views/CompetencyAssessments/SelectOptionalCompetencies.cshtml @@ -60,7 +60,11 @@ @foreach (var competency in competencyGroup) {
- +