I have several localStorage keys such as Recycle Bin, View, and Recycle Bin.
Question two times:
1) How can I execute a loop, although the identifier of an element in localStorage is the most efficient way, and if the identifier already exists, add a class or data attribute to the corresponding DOM element.
2) What would be the best way to create separate lists from the last 20 items from 3 localStorage Keys (Trash, Trash, Viewed)? In terms of performance, it would be better to do this in the same Loop function as pt1 of my question (for example, also checks if an identifier exists and adds a class if true)?
This is what I have:
HTML
<article data-id="548" data-date="Mon Dec 19 09:12:57"> <h1><a href="#">Title</a></h1> // section // footer <a href="#" data-context="trash">trash</a> </article>
JQuery
$tools.on("click", "a", function(e){ var storeId = $(this).attr('data-context'); var selector = $(this).closest('article'); var dataId = selector.attr('data-id'); var pubDate = selector.attr('data-date'); var postUrl = selector.find('a', 'h1').attr('href'); var postTitle = selector.find('h1').text(); var $removable = $container.find( selector ); setLocalStorage(storeId, dataId, postUrl, postTitle, pubDate); if (storeId == "Trash") { $container.isotope( 'remove', $removable ); }; e.preventDefault(); });
Function setter.
Variables are transmitted via data-context="Trash" (key) and data-id="001" (id) and stored in the corresponding key.
function setLocalStorage(itemKey, dataId, postUrl, postTitle, postPub) { var ItemKey = itemKey; var timeStamp = new Date(); var json = JSON.parse(localStorage.getItem(ItemKey)); if(json == null) json = []; json.push({id:dataId,title:postTitle,url:postUrl,postPub:postPub,dataTimeStamp:timeStamp});
LocalStorage ends as follows: Trash [{"id":"418","title":"\n\t\t\t\t\t\t\t\t\tPost Title....//..."}]
Any help here would be greatly appreciated.
Happy New Year!
Cheers Ben