JSFiddle - React, Tailwind, and code Playground
by kougiland
HTML
<div class="faveDiv">
<div id="faves"></div>
</div>
<div class="adder">
<input style="margin-top:40px" type="text" id="id" />
<button id='save'>save</button>
<button id='cleanLocalStorage'>cleanLocalStorage</button>
</div>
CSS
.faveDiv {
width:320px;
padding:5px
}
img {
border:1px #ccc solid;
}
a {
margin-right: 6px
}
.blog {
width: 60px;
float: left;
}
.adder {
float:left;
clear:both;
margin-top:30px
}
button {
width:48px
}
JavaScript
if(typeof(Storage)!=="undefined")
{
// Yes! localStorage and sessionStorage support!
localStrg();
}
else
{
// Sorry! No web storage support..
}
function localStrg(){
$("button#save").click(function () {
var id = $("#id").val();
if (id == "") {
alert('empty Field');
return false
}
// UPDATE
var result = JSON.parse(localStorage.getItem("blog")) || [];
var loc = result.filter(function (item) {
return item.id === id;
});
if (loc.length) {
return;
}
var text = 'http://' + id + '.tumblr.com';
result.push({
id: id,
tumblr: text
});
// SAVE
localStorage.setItem("blog", JSON.stringify(result));
// APPEND
$("#faves").append('<div class="blog"><button class="del" id=' + id + '>x</button><a target="_blank" href=' + text + '>' + imgstem + id + imgstemclose + '</a></div>');
});
// INIT
var blog = JSON.parse(localStorage.getItem("blog"));
var stem = 'http://'
var stemclose = '.tumblr.com';
var imgstem = '<img src="http://api.tumblr.com/v2/blog/'
var imgstemclose = '.tumblr.com/avatar/48"/>'
//console.log(blog[0].id);
if (blog != null) {
for (var i = 0; i < blog.length; i++) {
var item = blog[i];
var text = 'http://' + item.id + '.tumblr.com';
$("#faves").append('<div class="blog"><button class="del" id=' + item.id + '>x</button><a href=' + text + '>' + imgstem + item.id + imgstemclose + '</a></div>');
}
}
$("button#cleanLocalStorage").click(function () {
//Clear localStorage
window.localStorage.clear();
location.reload();
return false;
});
$('#faves').on('click', 'button.del', function (e) {
var id = $(e.target).attr('id');
// UPDATE
var blog = JSON.parse(localStorage.getItem("blog"));
//…could be rewritten to use square bracket syntax instead:
//var blog = JSON.parse(localStorage["blog"]);
var blog = blog.filter(function (item) {
return item.id !== id;
});
// SAVE
...