I have problems with cookies
Hello everyone!!!
I'm starting with jQuery and specially with cookies. As a test application I'm using a nested list with a collapsible effect created with jQuery.
The effect is working fine and now I'm trying the next level which is save the state of the list between reloads. For this I'm using cookies (jquery.cookie.js) but there is something strange that I can't understand.
The cookie is been saved in the browser but I can't get the value after reloading the page.
This is how my code looks like:
- <script type="text/javascript">
- $(document).ready(function() {
- $('ul li').children('ul').hide();
-
- var c = $.cookie(this.id);
- if(c === 'true') {
- console.log(c);
- alert('Value');
- } else {
- alert('no Value');
- }
-
- $('ul li').addClass('reveal');
-
- $('.reveal').each(function(i) {
- $(this).attr('id', 'li'+i++);
- });
-
- $('.reveal').click(function() {
- $(this).children('ul').slideDown();
- $.cookie(this.id, 'true');
- console.log($.cookie(this.id));
- });
- });
- </script>
-
- </head>
- <body>
-
- <ul>
- <li>
- <span>Header</span>
- <ul>
- <li><a href="page2.html">Page 2</a></li>
- <li>No link</li>
- <li><a href="page2.html">Page 2</a></li>
- </ul>
- </li>
- <li>Header</li>
- <li>Header</li>
- <li>
- <span>Header</span>
- <ul>
- <li><a href="page2.html">Page 2</a></li>
- <li>
- <span>Header</span>
- <ul>
- <li><a href="page2.html">Page 2</a></li>
- <li>No link</li>
- <li><a href="page2.html">Page 2</a></li>
- </ul>
- </li>
- <li><a href="page2.html">Page 2</a></li>
- </ul>
- </li>
-
- </ul>
- </body>
Of course I'm calling jQuery and jquery.cookies.js in the head before the $(document).ready
What I'm doing wrong?
Thanks for your help!!!