I am toggling between two views using LocalStorage. I can see the my set key and value in localstorage when I click the toggle button. But when I refresh, I lose the current view. What changes can I make to the code to save view in localStorage?
JavaScript
x
49
49
1
let dataType = localStorage.getItem("dataViewType");
2
const toggleData = () => {
3
const data = document.querySelectorAll("button.data");
4
const dataTable = document.querySelector(".dataTable");
5
const dataGraph = document.querySelector(".dataGraph");
6
7
8
if (dataTable.style.display === "block") {
9
dataTable.style.display = "none";
10
dataGraph.style.display = "block";
11
data.forEach(
12
(element) => (element.innerText = "View data as Table"),
13
localStorage.setItem("dataViewType", "block")
14
15
);
16
} else {
17
dataGraph.style.display = "none";
18
dataTable.style.display = "block";
19
data.forEach(
20
(element) => (element.innerText = "View data as Graph"),
21
localStorage.setItem("dataViewType", "none")
22
);
23
}
24
25
};
26
27
const toggleDataonClick = () => {
28
dataType = localStorage.getItem("dataViewType")
29
if (dataType) {
30
toggleData()
31
}
32
}
33
window.addEventListener("DOMContentLoaded", (event) => {
34
35
const data = document.querySelectorAll("button.data");
36
data.forEach((element) =>
37
element.addEventListener("click", (event) => TtoggleDataonClick()),
38
);
39
}
40
**CSS**
41
42
.dataTable {
43
display: none;
44
}
45
46
.dataGraph {
47
display: block;
48
}
49
Advertisement
Answer
I have refactored your code so that you can achieve to render the current view:
script.js
JavaScript
1
51
51
1
// The new states will be saved as such:
2
// table -> to display the dataTable element
3
// graph -> to display the dataGraph element
4
5
// Separate the render from the toggle
6
const renderView = (dataType) => {
7
const data = document.querySelectorAll("button.data");
8
const dataTable = document.querySelector(".dataTable");
9
const dataGraph = document.querySelector(".dataGraph");
10
dataTable.style.display = dataType === "table" ? "block" : "none";
11
dataGraph.style.display = dataType === "graph" ? "block" : "none";
12
const label = getLabel(dataType);
13
data.forEach((element) => {
14
element.innerText = `View data as ${label}`;
15
});
16
}
17
18
// A little function to get the next toggle state
19
const getNextType = (dataType) => {
20
return dataType === "table" ? "graph" : "table";
21
}
22
23
// A little function to get the current state label
24
const getLabel = (dataType) => {
25
return dataType === "table" ? "Table" : "Graph";
26
}
27
28
// The actual toggle function
29
const toggleDataOnClick = () => {
30
const dataType = localStorage.getItem("dataViewType");
31
if (dataType) {
32
// Get the next state
33
const nextType = getNextType(dataType);
34
// Store it in the localStorage
35
localStorage.setItem("dataViewType", nextType);
36
// Render the view
37
renderView(nextType);
38
}
39
}
40
41
// This event runs when the DOM content is loaded
42
window.addEventListener("DOMContentLoaded", (event) => {
43
// Get the current saved state
44
const dataType = localStorage.getItem("dataViewType");
45
// Render the view
46
renderView(dataType);
47
// Add the click event listeners for the view
48
const data = document.querySelectorAll("button.data");
49
data.forEach((element) => element.addEventListener("click", (event) => toggleDataOnClick()));
50
});
51
styles.css
JavaScript
1
8
1
.dataTable {
2
display: none;
3
}
4
5
.dataGraph {
6
display: block;
7
}
8
The script works with this HTML file:
index.html
JavaScript
1
16
16
1
<html>
2
<head>
3
<title>Toggle View</title>
4
<link rel="stylesheet" href="styles.css">
5
</head>
6
<body>
7
<div class="dataTable">
8
<button class="data"></button>
9
</div>
10
<div class="dataGraph">
11
<button class="data"></button>
12
</div>
13
<script src="script.js"></script>
14
</body>
15
</html>
16