diff --git a/app/javascript/agreements.js b/app/javascript/agreements.js index 7b465e116c3..9d9d5e7b7ba 100644 --- a/app/javascript/agreements.js +++ b/app/javascript/agreements.js @@ -4,15 +4,20 @@ document.addEventListener('DOMContentLoaded', () => { ) const submit = document.querySelector('.js-agreements-submit') - checkboxes.forEach((element) => { - element.addEventListener('click', () => { - const isSubmit = checkboxes.every((element) => element.checked) + const updateSubmitButtonStateFromCheckbox = () => { + const isSubmit = checkboxes.every((element) => element.checked) + if (isSubmit) { + submit.classList.remove('is-disabled') + } else { + submit.classList.add('is-disabled') + } + } - if (isSubmit) { - submit.classList.remove('is-disabled') - } else { - submit.classList.add('is-disabled') - } + if(checkboxes.length) { + checkboxes.forEach((element) => { + element.addEventListener('click', updateSubmitButtonStateFromCheckbox) }) - }) + + updateSubmitButtonStateFromCheckbox() + } })