-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpet.js
89 lines (65 loc) · 2.2 KB
/
pet.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
const modal = document.querySelector(".modal"); //selects the modal
const btnCloseModal = document.querySelector(".close-modal"); //selects the button to close the modal
const btnOpenModal = document.querySelector(".show-modal"); //selects the button to show the modal
const overlay = document.querySelector(".overlay"); //selects the overlay
const toggleModal = function () {
modal.classList.toggle("hidden");
overlay.classList.toggle("hidden");
};
btnOpenModal.addEventListener("click", toggleModal);
btnCloseModal.addEventListener("click", toggleModal);
overlay.addEventListener("click", toggleModal);
// Multi step and validation form
var currentTab = 0;
showTab(currentTab);
// This function displays the specified form tab and Previus/Next buttons
function showTab(n) {
let x = document.querySelectorAll(".tab");
x[n].style.display = "block";
if (n == 0) {
document.getElementById("prev-btn").style.display = "none";
} else {
document.getElementById("prev-btn").style.display = "inline";
}
if (n == (x.length - 1)) {
document.getElementById("next-btn").innerHTML = "Enviar";
} else {
document.getElementById("next-btn").innerHTML = "Próximo";
}
fixStepIndicator(n);
}
// This function checks which tab will be displayed
function nextPrev(n) {
let x = document.querySelectorAll(".tab");
if (n == 1 && !validateForm()) return false;
x[currentTab].style.display = "none";
currentTab = currentTab + n;
if (currentTab >= x.length) {
document.getElementById("pet-register-form").submit();
return false;
}
showTab(currentTab);
}
// This function validates the form fields
function validateForm() {
let x, y, i, valid = true;
x = document.querySelectorAll(".tab");
y = x[currentTab].getElementsByTagName("input");
for (i = 0; i < y.length; i++) {
if (y[i].value == "") {
y[i].className += " invalid";
valid = false;
}
}
if (valid) {
document.querySelectorAll(".step")[currentTab].className += " finish";
}
return valid;
}
function fixStepIndicator(n) {
let i, x = document.querySelectorAll(".step");
for (i = 0; i < x.length; i++) {
x[i].className = x[i].className.replace(" active", "");
}
x[n].className += " active";
}