rogerhnn rogerhnn - 1 month ago
206 0

I am trying to adapt the tables function to work with the list using data-ids.
I can save the toggle state to localstorage, but don't know why it is not reading.

Comment the function for LI and uncomment the function for the tables to see the tables function working.

Full code:

http://codepen.io/RogerHN/pen/yavpjQ

Javascript

Save toggle state to local storage

// Function for the LI
$('li').each(function() {
  var id = $(this).attr('data-id');
  var highlight = JSON.parse(localStorage[id] || "[]")
  if (highlight.length)
    $("li",this).each(function(i) {
      $(this).toggleClass("highlight", highlight[i])
    });
});
  $(".makethisred").click(function(e) {
    $(this).parent().parent().toggleClass("highlight");
    var list = $(this).parent().parent(),
      id = list.attr("data-id")
    localStorage[id] = JSON.stringify(
      $(list).map(function() {
        return $(this).hasClass("highlight")
      }).get()
    )
  });


/*
// Function for the tables
$("table[id]").each(function() {
    var id = this.id;
    var highlights = JSON.parse(localStorage[id] || "[]")
    if (highlights.length)
        $("tr",this).each(function(i) {
            $(this).toggleClass("highlight", highlights[i])
        });

});
$("tr").click(function(e) {
	if (e.target.nodeName==="INPUT") return;
    $(this).toggleClass("highlight");
    var table=$(this).closest("table"),
    	id=table.attr("id")
    localStorage[id] = JSON.stringify(
        $("tr",table).map(function() {
            return $(this).hasClass("highlight")
        }).get()
    )

});
*/
Comments