-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcounter.html
More file actions
42 lines (39 loc) · 1.15 KB
/
Copy pathcounter.html
File metadata and controls
42 lines (39 loc) · 1.15 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Counter</title>
</head>
<body>
<h1>Counter</h1>
<h2>Hello!</h2>
<p>You have clicked this button <b><span id="p">0</span></b> time(s).</p>
<button onclick="Counter()">Count</button>
<button>Count 2</button>
</body>
<script>
// to define a new variable use let keyword.
let c = 0;
function Counter() {
c += 1;
// alert(`Counter = ${c}`);
document.getElementById("p").innerText = c;
if (c % 2 === 0) {
document.querySelector('h1').innerHTML = "Goodbye!";
}
else{
document.querySelector('h1').innerHTML = "Welcome!";
}
// another way of doing the same thing
if (document.querySelector('h2').innerHTML === "Hello!") {
document.querySelector('h2').innerHTML = "Goodbye!";
}
else {
document.querySelector('h2').innerHTML = "Hello!";
}
}
// adding an event listener
document.querySelectorAll('button')[1].onclick = Counter;
</script>
</html>