-
-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathform.js
More file actions
173 lines (151 loc) · 5.64 KB
/
Copy pathform.js
File metadata and controls
173 lines (151 loc) · 5.64 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Form validation and interaction logic for application form
document.addEventListener('DOMContentLoaded', function () {
// Gender identity custom input toggle
document
.getElementById("gender-identity")
.addEventListener("change", function () {
const customGenderInput = document.getElementById("custom-gender");
if (this.value === "Other") {
customGenderInput.style.display = "block";
customGenderInput.removeAttribute("disabled");
this.removeAttribute("name");
customGenderInput.setAttribute("name", "gender-identity");
} else {
customGenderInput.style.display = "none";
customGenderInput.setAttribute("disabled", "true");
customGenderInput.removeAttribute("name");
this.setAttribute("name", "gender-identity");
}
});
// Pronouns custom input toggle
const pronounsCheckboxes = document.querySelectorAll(
"input[name='pronouns']",
);
const customPronounsInput = document.getElementById("custom-pronouns");
pronounsCheckboxes.forEach((checkbox) => {
checkbox.addEventListener("change", () => {
if (document.getElementById("pronouns-other").checked) {
customPronounsInput.style.display = "block";
customPronounsInput.removeAttribute("disabled");
customPronounsInput.setAttribute("name", "pronouns");
} else {
customPronounsInput.style.display = "none";
customPronounsInput.setAttribute("disabled", "true");
customPronounsInput.removeAttribute("name");
}
});
});
// Race/ethnicity custom input toggle
document
.getElementById("race-ethnicity")
.addEventListener("change", function () {
const customRaceEthnicityInput = document.getElementById(
"custom-race-ethnicity",
);
if (this.value === "Other") {
customRaceEthnicityInput.style.display = "block";
customRaceEthnicityInput.removeAttribute("disabled");
this.removeAttribute("name");
customRaceEthnicityInput.setAttribute("name", "custom-race-ethnicity");
} else {
customRaceEthnicityInput.style.display = "none";
customRaceEthnicityInput.setAttribute("disabled", "true");
customRaceEthnicityInput.removeAttribute("name");
this.setAttribute("name", "custom-race-ethnicity");
}
});
// Learn more expandable sections
const learnMoreWrappers = document.querySelectorAll(".learn-more-wrapper");
learnMoreWrappers.forEach((wrapper) => {
const title = wrapper.querySelector(".learn-more-title");
const content = wrapper.querySelector(".learn-more-content");
const toggleIcon = wrapper.querySelector(".toggle-icon");
toggleIcon.style.cursor = "pointer";
toggleIcon.querySelector(".toggle-icon-plus").style.display = "inline";
toggleIcon.querySelector(".toggle-icon-minus").style.display = "none";
content.style.display = "none";
let isExpanded = false;
title.addEventListener("click", () => {
if (!isExpanded) {
content.style.display = "block";
toggleIcon.querySelector(".toggle-icon-plus").style.display = "none";
toggleIcon.querySelector(".toggle-icon-minus").style.display =
"inline";
title.setAttribute("aria-expanded", true);
} else {
content.style.display = "none";
toggleIcon.querySelector(".toggle-icon-plus").style.display =
"inline";
toggleIcon.querySelector(".toggle-icon-minus").style.display = "none";
title.setAttribute("aria-expanded", false);
}
isExpanded = !isExpanded;
});
});
});
// Form validation function
function validateForm() {
const form = document.getElementById("app-form-1");
const requiredFields = form.querySelectorAll("[required]");
let allValid = true;
requiredFields.forEach((field) => {
const parentElement = field.parentNode;
parentElement.style.border = "";
field.style.border = "";
const tag = field.tagName.toUpperCase();
if (field.type === "radio" || field.type === "checkbox") {
const isChecked = form.querySelector(`[name="${field.name}"]:checked`);
if (!isChecked) {
allValid = false;
parentElement.style.border = "2px solid red";
}
} else if (tag === "SELECT") {
if (field.value === "") {
allValid = false;
field.style.border = "2px solid red";
}
} else if (
field.type === "text" ||
field.type === "email" ||
field.type === "tel" ||
field.type === "number" ||
tag === "TEXTAREA"
) {
if (!field.value.trim()) {
allValid = false;
field.style.border = "2px solid red";
}
}
});
if (!allValid) {
alert("Please fill out the missing field(s).");
} else {
window.location.href = "/app-household";
}
}
// participant assessment form save button input validation
function saveBtnValidation(){
document.addEventListener("DOMContentLoaded", function () {
const form = document.getElementById("participant_assessment");
saveButton.addEventListener("click", () => {
const requiredInputs = form.querySelectorAll("input[required]");
let isValid = true;
requiredInputs.forEach((input) => {
const parentElement = input.parentNode;
parentElement.style.border = "";
if (
(input.type === "radio" || input.type === "checkbox") &&
!form.querySelector(`[name="${input.name}"]:checked`)
) {
isValid = false;
parentElement.style.border = "2px solid red";
}
});
if (!isValid) {
alert("Please fill out all required fields.");
} else {
window.location.href = "/thankyou";
}
});
});
}