-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
60 lines (51 loc) · 1.98 KB
/
index.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
document.addEventListener("DOMContentLoaded", function () {
const form = document.querySelector(".form");
const submitButton = document.querySelector("#submitButton");
function collectFormData() {
const formData = {};
form.querySelectorAll("input, textarea").forEach(input => {
formData[input.id] = input.value;
});
return formData;
}
function toggleErrorStyle(isError) {
submitButton.innerText = isError ? "Заповніть усі обов'язкові поля *" : "Замовити демо";
submitButton.classList.toggle("submit_error", isError);
setTimeout(() => {
submitButton.innerText = "Замовити демо";
submitButton.classList.remove("submit_error");
}, 5000);
}
function handleFormSubmit(event) {
event.preventDefault();
const isAnyRequiredEmpty = ["name", "surname", "number"].some(fieldId => {
const input = document.querySelector(`#${fieldId}`);
return input.value.trim() === '';
});
if (isAnyRequiredEmpty) {
toggleErrorStyle(true);
return;
}
const formData = collectFormData();
// Further processing of form data
console.log(formData);
form.reset(); // Очистить поля формы
// Submit button animation
submitButton.classList.add("submit_onclic");
setTimeout(() => {
submitButton.classList.remove("submit_onclic");
submitButton.innerText = "✔";
setTimeout(() => {
submitButton.innerText = "Замовити демо";
}, 1250);
}, 2250);
}
// Event Listeners
form.addEventListener("submit", handleFormSubmit);
submitButton.addEventListener("click", handleFormSubmit);
form.addEventListener("keypress", event => {
if (event.key === "Enter") {
handleFormSubmit(event);
}
});
});