The problem I’m having is below the relevant code.
So I have this ‘movie’ list that is just a fancier to-do list. I’m saving the lists locally and it’s working fine. The lists save and load. I also have stamps that show that the movie is ‘approved’ by toggling a class on and off.
Here is the toggle function:
function approveMovie() {
if(this.parentElement.id === "listDanny") {
this.children[1].innerText = "Lola approved!";
this.children[1].classList.toggle('approvedVisible');
}
if(this.parentElement.id === "listLola") {
this.children[1].innerText = "Danny approved!";
this.children[1].classList.toggle('approvedVisible');
}
}
And here are the classes I’m using. As you can see, to make the stamp visible I just toggle the .approvedVisible
class on and off.
.approvedHidden {
opacity: 0;
position: absolute;
left: 175px;
top: -10px;
font-size: 20px;
color: green;
transform: skew(0deg, 170deg);
}
.approvedVisible {
opacity: 1;
}
And here are the save and load functions:
function saveFunk(state1, state2) {
localStorage.setItem('dannyMovies', JSON.stringify(dannyArray));
localStorage.setItem('lolaMovies', JSON.stringify(lolaArray));
localStorage.setItem('dannyStates', JSON.stringify(state1));
localStorage.setItem('lolaStates', JSON.stringify(state2));
console.log('states: ', state1, state2);
}
window.onload = function() {
dannyArray = Array.from(JSON.parse(localStorage.getItem('dannyMovies')));
lolaArray = Array.from(JSON.parse(localStorage.getItem('lolaMovies')));
dannyArray.forEach(movie => addItem("Danny", movie));
lolaArray.forEach(movie => addItem("Lola", movie));
dannyStampStates = Array.from(JSON.parse(localStorage.getItem('dannyStates')));
lolaStampStates = Array.from(JSON.parse(localStorage.getItem('lolaStates')));
assignStamps(listDanny, dannyStampStates);
assignStamps(listLola, lolaStampStates);
console.log('states2: ', dannyStampStates, lolaStampStates);
}
So…I have another function that is saving the states of the stamps, whether they are on or off, and am saving them in the saveFunk
above(the StampStates). As you can see in the onload
function, the saved lists are parsed and then re-added to the list. Then my stamp states are parsed and passed into the assignStamps
function. The assignStamps
function re-adds the approvedVisible
class to each of the list items according to their previous saved states.
And this works. When I re-load the page, the lists are added like they should, and the classes are added but remain hidden. I can inspect each list item that is supposed to have the approvedVisible
class, and they do have them. The problem is that they don’t appear. If I then click on a list item that has the class, the class gets toggled off. I click again, and the class is toggled on and appears.
So that’s my problem. How do I get the stamp to appear on load, even when the class is getting added like it should. I’m suspicious that it has something to do with the toggle, but I’m not sure.
Sorry for the long explanation for such a short problem