-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
61 lines (56 loc) · 1.78 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
window.addEventListener("DOMContentLoaded", function () {
//counter
//#region
let personsInput = document.querySelectorAll(".counter-block-input")[0],
daysInput = document.querySelectorAll(".counter-block-input")[1],
placeInput = document.getElementById("select"),
priceOutput = document.getElementById("total");
//values for calculation**
const data = {
persons: 0,
days: 0,
priceToShow: 0,
locationPrice: 1,
// to change according to price 1 day for 1 person.
//see html -> id="select" -> value="1" to increase costs for other places
servicePrice: 150,
};
//**values for calculation
// function to check input != 0 or ""
function checkInput() {
return (
daysInput.value == "" ||
personsInput.value == "" ||
daysInput.value == "0" ||
personsInput.value == "0"
);
}
//function to update values for calculation
const updateState = (input, value) => {
data[value] = input.value;
data.priceToShow =
data.persons * data.days * data.locationPrice * data.servicePrice;
};
// change event on persons field
personsInput.addEventListener("change", () => {
updateState(personsInput, "persons");
checkInput()
? (priceOutput.innerText = 0)
: (priceOutput.innerText = `${data.priceToShow}$`);
});
// change event on days field
daysInput.addEventListener("change", () => {
updateState(daysInput, "days");
checkInput()
? (priceOutput.innerText = 0)
: (priceOutput.innerText = `${data.priceToShow}$`);
});
// change event on places field
placeInput.addEventListener("change", () => {
updateState(placeInput, "locationPrice");
checkInput()
? (priceOutput.innerText = 0)
: (priceOutput.innerText = `${data.priceToShow}$`);
});
//#endregion
});