I'm currently creating a simple todo list, I'd appreciate if someone could help me with cookies :)
I've got 2 functions to set and get cookies:
function setCookie(key, value) {
var expires = new Date();
expires.setTime(expires.getTime() + 31536000000);
document.cookie = key + '=' + value + ';expires=' + expires.toUTCString();
}
function getCookie(key) {
var keyValue = document.cookie.match('(^|;) ?' + key + '=([^;]*)(;|$)');
return keyValue ? keyValue[2] : null;
}
There's a button (with id #add_todo ) to add a task into todo list (with class .todo_list). I set the value of text input $('#todo_description').val() into cookies.
$(document).ready( function() {
$('#add_todo').click( function() {
setCookie('test', $('#todo_description').val());
var todoDescription = $('#todo_description').val();
$('.todo_list').prepend(
div class="todo"
+checkbox //I avoided html brackets here
+ getCookie('test')
+delete_button
/div
);
});
Everything works fine but when i reload the web page the tasks i added before are not displayed, and i understand i do it wrong. I'd like to store whole div (class="todo") which contains todoDescription, checkbox, delete button and stylings. I tried to store tasks in array but couldn't retrieve its elements one by one.. thanks in advance!