Skip to content

Commit bde4533

Browse files
committed
DOM with localStorage
1 parent 177599c commit bde4533

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

DOM.html

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>DOM Manipulation</title>
5+
<style>
6+
body {
7+
text-align: center;
8+
}
9+
10+
#my-button {
11+
height: 50px;
12+
width: 50px;
13+
font-size: 25px;
14+
border: none;
15+
border-radius: 5px;
16+
}
17+
18+
.odd {
19+
background-color: aqua;
20+
}
21+
.even {
22+
background-color: lawngreen;
23+
}
24+
</style>
25+
</head>
26+
<body>
27+
<h2>Times of Button Pressed</h2>
28+
<button id="my-button" onclick="buttonPressed()"></button>
29+
30+
<script>
31+
//let noOfTimesClicked = 0;
32+
33+
let noOfTimesClicked = localStorage.getItem("noOfTimesClicked") || 0;
34+
35+
function buttonPressed() {
36+
noOfTimesClicked++;
37+
38+
localStorage.setItem("noOfTimesClicked", noOfTimesClicked);
39+
40+
updateButton();
41+
}
42+
43+
function updateButton() {
44+
let button = document.querySelector("#my-button");
45+
46+
if (noOfTimesClicked % 2 === 0) {
47+
button.classList.add("even");
48+
button.classList.remove("odd");
49+
} else {
50+
button.classList.remove("even");
51+
button.classList.add("odd");
52+
}
53+
54+
button.innerText = noOfTimesClicked;
55+
}
56+
57+
updateButton();
58+
</script>
59+
</body>
60+
</html>

0 commit comments

Comments
 (0)