简体   繁体   中英

how to retrieve from cookies specified data? JQuery

im currently working on a simple todo list. I store the tasks in cookies and when the web page reloads i retrieve my previous tasks from the cookies. However i cannot save there my tasks as completed, ie the tasks that i marked as complete.

function toHTML(id, text) {

    return '<div class="todo" id="' + id + '">' +
        '<div id="' + cbid + '"><input type="checkbox" class="check_todo" name="check_todo"/></div>' +
        '<div class="todo_description" contentEditable = "true">' +
            text +
        '</div>' +
        '<img src = "sun-icon.gif" class="close" title = "Delete the Task" />' +
    '</div>';
}

When i press checkbox the div with class = "todo" gets class = "checked" and the text (todo_description) inside this div becomes crossed, also the div with class="todo" itself gets another color.

$('#item-' + counter +' .check_todo').unbind('click');
    $('#item-' + counter +' .check_todo').click( function() {   
        var todo = $(this).parent().parent();
        todo.toggleClass('checked');

    });

When i reload the page i see my previous tasks but not completed tasks, because i retrieve from cookies only text:

 function get_cookies_array(){ 

        var cookies = { };
        if (document.cookie && document.cookie != '') {

            var split = document.cookie.split(';');
            for (var i = 0; i < split.length; i++) {
                var name_value = split[i].split("=");
                name_value[0] = name_value[0].replace(/^ /, '');
                cookies[decodeURIComponent(name_value[0])] = decodeURIComponent(name_value[1]);
            } 
        }
        return cookies;   
    }

//here i display my tasks

var cookies = get_cookies_array();
        var i = 0;
        for(var name in cookies) {
            $('.todo_list').prepend(toHTML('item-' + (i++), cookies[name]));
             }

I tried to check for the class "checked" like if $(cookies[name]).hasClass('checked') { make stylings for this div} but this doesnt work, i need to check for div with class = "todo" which becomes "todo checked" when i press checkbox.

I'd appriciate if someone could help me!

if you want to check if checkbox is checked you can try :

$('#id_checkbox').is(':checked'); 

if you want to get your cookie without reloading your page you can setTimeout between the set and the get of cookiees

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM