todo using localstorage
Created
October 7, 2016 05:02
-
-
Save bonaxcrimo/c8d8d176dfc1fd24eae5e6a094c858c7 to your computer and use it in GitHub Desktop.
todo with localstorage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="todo" class="container col-md-4 col-md-offset-4"> | |
<h1>TODO APP</h1> | |
<ul id="listTodo" class="list-group"> | |
</ul> | |
<input id="nama" type="text" name="add" class="form-control" placeholder="Enter to add TODO"> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function(){ | |
var cekSave = localStorage.getItem('todos'); | |
var dataList = (cekSave!=null)?JSON.parse(cekSave):[]; | |
var localSet = function(){ | |
localStorage.setItem('todos',JSON.stringify(dataList)); | |
} | |
localSet(); | |
var dat=""; | |
for(var i=0;i<dataList.length;i++){ | |
dat +="<li class='list-group-item'><a href='' class='badge'>Delete</a><span>"+dataList[i]+"</span></li>"; | |
} | |
$("#listTodo").append(dat); | |
$("#nama").on('keyup',function(e){ | |
if(e.keyCode == 13){ | |
if($("#nama").val().trim().length > 0){ | |
$("#listTodo").append("<li class='list-group-item'><a href='' class='badge'>Delete</a><span>"+$("#nama").val()+"</span></li>"); | |
dataList.push($("#nama").val()); | |
$("#nama").val(''); | |
}else alert("tidak boleh kosong"); | |
// console.log(dataList); | |
localSet(); | |
} | |
}); | |
$("a.badge").click(function(){ | |
var x = $(this).index('a.badge'); | |
// console.log(x); | |
dataList.splice(x,1); | |
$("li").eq(x).remove(); | |
localSet(); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment