From 28e5b39962f7e2ed1409717b3c381342bfb2d479 Mon Sep 17 00:00:00 2001 From: Dan Remollino Date: Sat, 12 Apr 2025 11:29:12 -0400 Subject: [PATCH] refactored dependant field check --- validation.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/validation.js b/validation.js index edf4f0a..7d75e57 100644 --- a/validation.js +++ b/validation.js @@ -98,16 +98,21 @@ class ValidationField { // dependant fields if (this.dependant) { - const parent = this.validationInstance.form.querySelector(`[name=${this.dependant}]`) + const parents = this.validationInstance.form.querySelectorAll(`[name="${this.dependant}"]`) const depCheck = () => { - parent.value - ? this.el.removeAttribute('disabled') - : this.el.setAttribute('disabled', 'disabled') + if (((parents[0].type === 'checkbox' || parents[0].type === 'radio') && this.validationInstance.form.querySelectorAll(`[name="${this.dependant}"]:checked`).length > 0) || + (((parents[0].type !== 'checkbox' && parents[0].type !== 'radio') && parents[0].value))) { + this.el.removeAttribute('disabled') + } else { + this.el.setAttribute('disabled', 'disabled') + } } depCheck() - parent.addEventListener('input', depCheck) + for (const parent of parents) { + parent.addEventListener('input', depCheck) + } } // add required indicator