1

I'm currently creating a simple todo list, I'm having a trouble with cookies. When i remove the line $.cookie(todoDescription+1, todoDescription); the button to add a task works, and the new task is added to the list. But when i leave this line in the web page blinks and nothing happens.

   $(document).ready( function() {  

    showCookies();            // to show previous tasks when page is reloaded
    var all =0;

        $('#add_todo').click( function() {                 // button that adds a task

        var cookies = get_cookies_array() ;

        var todoDescription = $('#todo_description').val();   // string from textinput
           var mykey = todoDescription + 1;            //i jst decided to have such key

         $.cookie(todoDescription+1, todoDescription);     //this line doesnt work!

            //add task
            $('.todo_list').prepend(
            '<div class="todo">'
                + '<div>'
                    + '<input type="checkbox" id = "cb" class="check_todo" name="check_todo"/>'
                + '</div>'

                + '<div class="todo_description" contentEditable = "true">'
                    + todoDescription
                + '</div>'

                +'<div id = "delete">' +'<input id = "x" type = "submit" value = "X" onclick = "$.cookie('todoDescription+1',null);$(this).parent().parent().remove();"/>'+ '</div>'
            +'</div>');

           return false;



        }); //end add todo



    });

    function showCookies()
    {

    var cookies = get_cookies_array() ;
        for(var name in cookies) {
        if(name == cookies[name]+1){
             $('.todo_list').prepend(
            '<div class="todo">'
                + '<div>'
                    + '<input type="checkbox" id = "cb" class="check_todo" name="check_todo"/>'
                + '</div>'

                + '<div class="todo_description" contentEditable = "true">'
                    + cookies[name]
                + '</div>'

                +'<div id = "delete">' +'<input id = "x" type = "submit" value = "X" onclick = "$.cookie('name',null);$(this).parent().parent().remove();"/>'+ '</div>'
            +'</div>');
        }
        }

    }
     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;   
    }

I'd appreciate it if someone could help me.

0

1 Answer 1

1

Following is the description of Usage of jQuery cookie

Create session cookie:

$.cookie('the_cookie', 'the_value');

Create expiring cookie, 7 days from then:

$.cookie('the_cookie', 'the_value', { expires: 7 });

Create expiring cookie, valid across entire site:

$.cookie('the_cookie', 'the_value', { expires: 7, path: '/' });

Read cookie:

$.cookie('the_cookie'); // => "the_value"
$.cookie('the_cookie', { raw: true }); // => "the_value" not URL decoded
$.cookie('not_existing'); // => null

Delete cookie:

// returns false => No cookie found
// returns true  => A cookie was found
$.removeCookie('the_cookie'[, options]);

Note: when deleting a cookie, you must pass the exact same path, domain and secure options that were used to set the cookie, unless you're relying on the default options that is.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.