-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathform.js
More file actions
40 lines (35 loc) · 1.1 KB
/
form.js
File metadata and controls
40 lines (35 loc) · 1.1 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
const multiStepForm = document.querySelector("[data-multi-step]");
const formSteps = [...multiStepForm.querySelectorAll("[data-step]")];
let currentStep = formSteps.findIndex(step => {
return step.classList.contains("active")
})
if (currentStep < 0) {
currentStep = 0
showCurrentStep()
}
multiStepForm.addEventListener("click", e => {
let incrementor;
if (e.target.matches("[data-next]")) {
incrementor = 1;
} else if (e.target.matches("[data-previous]")) {
incrementor = -1;
}
if (incrementor == null) return;
const inputs = [...formSteps[currentStep].querySelectorAll("input")];
const allValid = inputs.every(input => input.reportValidity());
if (allValid) {
setTimeout(() => {
currentStep += incrementor;
showCurrentStep();
}, 1000); // Delay of 1 seconds (1000 milliseconds)
}
});
function showCurrentStep() {
formSteps.forEach((step, index) => {
step.classList.toggle("active", index === currentStep);
});
}
function handleSubmit (e) {
e.preventDefault();
alert('Successfully registered')
}