-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
113 lines (93 loc) · 3.45 KB
/
script.js
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
const multiStepForm = document.querySelector('[data-multi-step]');
const formSteps = [...multiStepForm.querySelectorAll('[data-step]')];
const steps = document.querySelectorAll('.step-number');
const stepsDiv = document.querySelector('.step');
let currentStep = formSteps.findIndex(step => step.classList.contains('active'));
let formData = {}; // To store all form data
let selectedOptions = []; // To store selected options
// Set the initial step text
stepsDiv.querySelector('p').innerHTML = `Step ${currentStep + 1} of 3`;
if (currentStep < 0) {
currentStep = 0;
formSteps[currentStep].classList.add('active');
showStepForm();
}
// Handle selection of options
let selectBox = document.querySelectorAll('.select-box');
selectBox.forEach(div => {
div.addEventListener('click', () => {
div.classList.toggle('active');
const index = selectedOptions.indexOf(div.textContent.trim());
if (index === -1) {
selectedOptions.push(div.textContent.trim());
} else {
selectedOptions.splice(index, 1);
}
console.log(selectedOptions);
});
});
// Multi-step form navigation
multiStepForm.addEventListener('click', e => {
e.preventDefault();
let incrementor = null;
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());
// Collect all input values
inputs.forEach(input => {
formData[input.name] = input.value;
});
console.log(formData);
if (allValid) {
currentStep += incrementor;
showStepForm();
updateFormSteps();
}
// If it's the last step, show the summary
if (currentStep === formSteps.length - 1) {
showSummary();
}
});
// Function to show the current step form
function showStepForm() {
formSteps.forEach((step, index) => step.classList.toggle('active', index === currentStep));
}
// Function to display the summary
function showSummary() {
const summaryDiv = document.querySelector('.card:nth-of-type(3) .form-group');
// Display form data
for (const key in formData) {
let data = document.createElement('p');
let titleOfData = document.createElement('span');
titleOfData.textContent = `${key}:`;
data.appendChild(titleOfData);
data.innerHTML += formData[key];
summaryDiv.appendChild(data);
}
// Display selected options
let titleOfData = document.createElement('span');
titleOfData.textContent = `topics:`;
summaryDiv.appendChild(titleOfData);
let list = document.createElement('ul');
summaryDiv.appendChild(list);
selectedOptions.forEach(option => {
let item = document.createElement('li');
item.textContent = option;
list.appendChild(item);
});
}
// Function to update step indicators
function updateFormSteps() {
stepsDiv.querySelector('p').innerHTML = `Step ${currentStep + 1} of 3`;
steps[currentStep].classList.add('active');
}
// Handle summary button click
const summaryBtn = document.querySelector('.card:nth-of-type(3) button');
summaryBtn.addEventListener('click', () => {
alert('Success -_-');
});