- (function() {
- //localStorage.clear();
- // Parse the JSON stored in allEntries
- var existingEntries = JSON.parse(localStorage.getItem("allEntries"));
- if(existingEntries == null) existingEntries = [];
- var entry = $(".entry").text();
-
- localStorage.setItem("entry", JSON.stringify(entry));
- // Save allEntries back to local storage
- existingEntries.push(entry);
- localStorage.setItem("allEntries", JSON.stringify(existingEntries));
-
- })();
How can I go about stopping the addition of a localStorage item if it already exists?
I had this snippet in the code but wasnt working.
- var getAll = JSON.parse(localStorage.getItem("existingEntries"));
for(var i = 0; i < getAll.length; i++){
//console.log(getAll[i]);
//
var x = getAll[i];
if (entry == x){
//then dont setItem
}}
- https://codepen.io/shawnbks9090/pen/vjgyzo
this is the only html:
- <span class="entry">hello</span>