test.html
<html>
<head>
<title>My New Page!</title>
<style></style>
</head>
<body>
<a href="test2.html" style="text-align: center;">Test 2</a>
<button id="button">Test 1</button>
</body>
<script>
const button = document.getElementById("button")
button.addEventListener("click", () => {button.innerHTML = "Changed 1"})
</script>
</html>
test2.html
<html>
<head>
<title>My New Page!</title>
<style></style>
</head>
<body>
<a href="test.html" style="text-align: center;">Test 1</a>
<button id="button">Test 2</button>
</body>
<script>
const button = document.getElementById("button")
button.addEventListener("click", () => {button.innerHTML = "Changed 2"})
</script>
</html>
Lets say I have 2 independent files, both will change the text of the button when they click on them.
(I have deliberately used the same ID value).
I would like to be able to switch between these pages without losing the changes. One way I can do this is I can add a link href link pointing to the other file for each file (as shown in the code), but that will cause us to lose the changes.
Is it possible to do this while differentiating the ID values (even though the id’s of the buttons are the same)? I was considering using iframe tags for each file, inside a "universal" file but I want to know if there was another way.
2
Answers
You can save any string data in
sessionStorage
orlocalStorage
. An interesting option would be providing some generic serialize function like this:index.html
page2.html