-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10-dom-projects.html
135 lines (96 loc) · 2.92 KB
/
10-dom-projects.html
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<!DOCTYPE html>
<head>
<title>DOM Projects</title>
<style>
body {
font-family: Arial;
}
.subscribe-button {
border: none;
background-color: black;
color: white;
border-radius: 2px;
padding-top: 10px;
padding-bottom: 10px;
padding-right: 15px;
padding-left: 15px;
font-weight: bold;
border-radius: 50px;
cursor: pointer;
margin-bottom: 30px;
}
.is-subscribed {
background-color: rgb(240, 240, 240);;
color: black;
}
.cost-input {
font-size: 15px;
padding: 10px;
}
.calculate-button {
background-color: green;
color: white;
border: none;
font-size: 15px;
padding: 12px 15px;
cursor: pointer;
}
</style>
</head>
<body>
<p> YouTube Subscribe Button</p>
<button onclick="
Subscribe();
" class="js-subscribe-button subscribe-button" >Subscribe</button>
<p>
Amazon Shipping Calculator
</p>
<input placeholder="Cost of order" class="js-cost-input cost-input" onkeydown="
handleCostKeydown(event);
">
<button onclick="
calculateTotal();
" class="calculate-button">Calculate</button>
<p class="js-total-cost"></p>
<script>
// number to string
String(25);
// dont do math in the following way:
// console.log('25'-5);
// console.log('25'+5);
// window represents everything in the window of the browser
// document means the following:
window.document
// consolee.log is actually the following:
window.console.log('window');
function handleCostKeydown(event) {
if(event.key === 'Enter'){
calculateTotal();
}
}
function calculateTotal() {
const inputElement = document.querySelector('.js-cost-input');
// we use innerHTML to get the inner HTML text in js, but since input element doesnt have any text we use .value
// when we get value from DOM it is a string >> so we have to switch from string to number:
let cost = Number(inputElement.value);
if (cost < 40) {
cost = cost + 10;
}
document.querySelector('.js-total-cost')
.innerHTML = `$${cost}`;
}
function Subscribe() {
const buttonElement = document.querySelector('.js-subscribe-button');
// inner Text gets u the text so there is no problem if there are extra spaces before or after the name.
if(buttonElement.innerText === 'Subscribe'){
buttonElement.innerHTML = 'Subscribed';
buttonElement.classList.add('is-subscribed');
}
else{
buttonElement.innerHTML = 'Subscribe';
buttonElement.classList.remove('is-subscribed');
}
}
</script>
</body>
</html>