General Application

(function () { // ─── Shared factory ─────────────────────────────────────────────────────── function createFieldToggle({ listenSelector, hiddenSelectors, messageText, messageClass, messageId }) { function injectMessage() { if (!messageText || document.getElementById(messageId)) return; const msg = document.createElement('div'); msg.id = messageId; msg.className = messageClass; msg.setAttribute('role', 'alert'); msg.setAttribute('aria-live', 'polite'); msg.setAttribute('aria-atomic', 'true'); msg.style.display = 'none'; msg.textContent = messageText; const anchor = document.querySelector(listenSelector); if (anchor) anchor.insertAdjacentElement('afterend', msg); } function showMessage() { const msg = document.getElementById(messageId); if (msg) msg.style.display = ''; } function hideMessage() { const msg = document.getElementById(messageId); if (msg) msg.style.display = 'none'; } function getElements() { return hiddenSelectors.flatMap(sel => [...document.querySelectorAll(sel)]); } function hideFields() { getElements().forEach(el => el.style.display = 'none'); } function restoreFields() { getElements().forEach(el => el.style.display = ''); } function handleChange(e) { const radio = e.target; if (!radio || radio.type !== 'radio') return; const label = radio.closest('label') || document.querySelector(`label[for="${radio.id}"]`); const labelText = label ? label.textContent.trim().toLowerCase() : ''; const isNo = labelText === 'no' || radio.value.toLowerCase() === 'no'; if (isNo) { showMessage(); hideFields(); } else { hideMessage(); restoreFields(); } } function init() { injectMessage(); const fieldLi = document.querySelector(listenSelector); if (!fieldLi) { console.warn(`TCG form: "${listenSelector}" not found.`); return; } fieldLi.addEventListener('change', handleChange); } return { init }; } // ─── Field 1: Work eligibility (li#field_1_7) ───────────────────────────── createFieldToggle({ listenSelector : 'li#field_1_7', messageId : 'work-eligibility-message', messageClass : 'work-message', messageText : 'We cannot accept your application if you are not legally entitled to work in Canada.', hiddenSelectors: [ '#field_1_8', '#field_1_9', '#field_1_48', '#gfield_description_1_48', '.ginput_container.ginput_container_checkbox', '.gform-footer.gform_footer.top_label', ], }).init(); // ─── Field 2: li#field_1_23 ─────────────────────────────────────────────── // Generate #field_1_24 through #field_1_48 programmatically const field23HiddenSelectors = [ ...Array.from({ length: 25 }, (_, i) => `#field_1_${i + 24}`), '#gfield_description_1_48', '#gfield_description_1_48 ~ .ginput_container.ginput_container_checkbox', '.gform-footer.gform_footer.top_label', ]; createFieldToggle({ listenSelector : 'li#field_1_23', messageId : 'field-23-message', messageClass : 'work-message', messageText : 'We cannot accept your application if you are not legally entitled to work in Canada.', hiddenSelectors: field23HiddenSelectors, }).init(); })();