I have several tabs in html / js, in each of these I have a form with the save change button, when I click on the save changes button the page is refreshaled. After the refresh page, the tab returns to the first item, while it should remain the same for the last time.
Is there any way to do this ?
I was thinking about localstorage but I’m not sure how to do it, can someone point me the right way ?
function openCity(evt, cityName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(cityName).style.display = "block";
evt.currentTarget.className += " active";
}
// Get the element with id="defaultOpen" and click on it
document.getElementById("defaultOpen").click();
/* Style the tab */
.tab {
float: left;
border: 1px solid #ccc;
background-color: #f1f1f1;
width: 30%;
height: 300px;
}
/* Style the buttons inside the tab */
.tab button {
display: block;
background-color: inherit;
color: black;
padding: 22px 16px;
width: 100%;
border: none;
outline: none;
text-align: left;
cursor: pointer;
transition: 0.3s;
font-size: 17px;
}
/* Change background color of buttons on hover */
.tab button:hover {
background-color: #ddd;
}
/* Create an active/current "tab button" class */
.tab button.active {
background-color: #ccc;
}
/* Style the tab content */
.tabcontent {
float: left;
padding: 0px 12px;
border: 1px solid #ccc;
width: 70%;
border-left: none;
height: 300px;
}
<div class="tab">
<button class="tablinks" onclick="openCity(event, 'London')" id="defaultOpen">London</button>
<button class="tablinks" onclick="openCity(event, 'Paris')">Paris</button>
<button class="tablinks" onclick="openCity(event, 'Tokyo')">Tokyo</button>
</div>
<div id="London" class="tabcontent">
<h3>London</h3>
<p>London is the capital city of England.</p>
</div>
<div id="Paris" class="tabcontent">
<h3>Paris</h3>
<p>Paris is the capital of France.</p>
</div>
<div id="Tokyo" class="tabcontent">
<h3>Tokyo</h3>
<p>Tokyo is the capital of Japan.</p>
</div>
5
Answers
You could add something like
to your
openCity()
function which will write the current tab id to localStorage. Then, on the page load, read from localStorage:Alternatively, you could look at using URL for state management, for example appending
currentCity
to the URL and reading from it on load.If you really want to use localStorage (which is probably not the best approach, as pointed out, using some hash params looks ‘better’), here is a proposal with minimal modification that could enable what you want
1- store the last selected city in the local storage when openCity is called
2- Add some script to be executed when the page opens where you will: read the value from the local storage; find the corresponding DOM element and trigger selection on it (and handle the case where nothing is stored by simply selecting the default select like you were doing before):
Note: adding an ‘id’ to your buttons and storing the id in the local storage would easen the retrieval of the last selected element.
you can find a working sample on this code pen: https://codepen.io/aSH-uncover/pen/qBYaZOB
Observation : As per your current code, Looks like you are triggering a
click()
event on element which containing an iddefaultOpen
every time page loads.Solution : This logic needs to be changed. We can achieve this requirement by using
Web storage API
by checking if there is anycurrentTab
variable available in local storage or not, If available load that tab else run this default click event logic.Now the steps will be :
Set the localStorage value inside openCity() method.
Check if there is any
currentTab
variable exist in local storage or not.Now add the below condition.
Live Demo (Due to security reasons, window.localStorage is not working in below code snippet but it will work fine locally or in jsfiddle) :
You will need to use local storage for this.
When page loads, get cityName from local storage with
Hope it helps!
when open city method is called at that time we need to store last selected city in local storage
when page load get the current city name from local store and add
Hope it will help for you!