Friday, 27 September 2013

How can I add a delete function to this code that stores stuff in localStorage?

How can I add a delete function to this code that stores stuff in
localStorage?

I'm adding names of blogs to localStorage. I use those to make links and
get avatars.
Here's a fiddle that does the adds but I need a nice delete function for
individual entries.
Here's part of what's at the fiddle:
$("button").click(function () {
var id = $("#id").val();
if (id != "") {
var text = 'http://' + id + '.tumblr.com';
} else {
alert('empty');
return false
}
// UPDATE
var blog = JSON.parse(localStorage.getItem("result"));
if (result == null) result = [];
result.push({
id: id,
tumblr: text
});
// SAVE
localStorage.setItem("blog", JSON.stringify(result));
// APPEND
$("#faves").append($("<a></a>").attr("href", text).html(imgstem + id +
imgstemclose));
});
// INIT
var result = JSON.parse(localStorage.getItem("result"));
var stem = 'http://'
var stemclose = '.tumblr.com';
var imgstem = '<img src="http://api.tumblr.com/v2/blog/'
var imgstemclose = '.tumblr.com/avatar/48"/>'
if (result != null) {
for (var i = 0; i < blog.length; i++) {
var item = blog[i];
$("#faves").append($("<a></a>").attr("href", stem + item.id +
stemclose).html(imgstem + item.id + imgstemclose));
}
}
I'm still learning every day so please feel free to make recommendations
that will improve this first pass.
Thanks!

No comments:

Post a Comment