Russell
Russell

Reputation: 665

setting localStorage to existing localStorage variables

Based on what I have read I would expect the following code to work.

This code is part of a function that is supposed to delete a localStorage variable by overwriting it with the next higher localStorage variable. Once there is no more to copy, the final variable is deleted.

'cc' elsewhere counts how many of a certain div is present (each div has three types of localStorage variables associated with it).

'x' holds the array position of the div that was clicked.

All that occurs is the "alert". Is the code inherently wrong, or have I made a mistake elsewhere?

for (z = x; z < cc; z++) {
    alert(z + " " + cc);
    localStorage.setItem("names" + z), localStorage.getItem("names" + (z + 1));
    localStorage.setItem("skillLevel" + z), localStorage.getItem("names" + (z + 1));
    localStorage.setItem("title" + z), localStorage.getItem("names" + (z + 1));
    localStorage.removeItem("names" + (z + 1));
    localStorage.removeItem("skillLevel" + (z + 1));
    localStorage.removeItem("title" + (z + 1));
}

Upvotes: 0

Views: 99

Answers (1)

tymeJV
tymeJV

Reputation: 104775

Your setItem seems to be throwing some syntax errors, you close off the ) too soon:

localStorage.setItem("names" + z), localStorage.getItem("names" + (z + 1));
                                ^
                                That ends the setItem func right there

I believe you want:

localStorage.setItem("names" + z, localStorage.getItem("names" + (z + 1)));

Upvotes: 2

Related Questions