File size: 1,383 Bytes
fa06092
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
document.addEventListener('DOMContentLoaded', function() {
    const numChildrenSelect = document.getElementById('num_children');
    
    if (numChildrenSelect) {
        // Run once on page load to initialize visibility
        toggleChildrenFields(parseInt(numChildrenSelect.value) || 0);
        
        // Listen for changes
        numChildrenSelect.addEventListener('change', function() {
            toggleChildrenFields(parseInt(this.value) || 0);
        });
    }
});

function toggleChildrenFields(count) {
    // We support 0 to 4 children
    for (let i = 1; i <= 4; i++) {
        const section = document.getElementById(`child_section_${i}`);
        if (section) {
            if (i <= count) {
                section.style.display = 'block';
                // Make inputs required if visible (optional, but let's let them be optional)
                // toggleInputsRequired(section, true);
            } else {
                section.style.display = 'none';
                // toggleInputsRequired(section, false);
            }
        }
    }
}

function toggleInputsRequired(container, isRequired) {
    const inputs = container.querySelectorAll('input');
    inputs.forEach(input => {
        // We only make the Name required if the section is shown
        if (input.name.endsWith('_name')) {
            input.required = isRequired;
        }
    });
}