RyanScottLewis
RyanScottLewis

Reputation: 14016

jQuery: Save an array of elements to cookie

I'm making a jquery plugin to create 'spindowns', you can locate the project at http://github.com/c00lryguy/jquery.spindown.js

The plugin works perfectly, but the only way I could figure out how to save which elements were expanded was to save an array filled with the expanded elements inner text.

Now, there are obvious downfalls to this. For instance, if two elements have the same text and one is expanded and the other wasn't, when it loads it from a cookie, it will think both of them were expanded.

Is there a good way to save an array of elements to a cookie?

Edit


No no, you guys don't seem to understand. Saving to a cookie works perfectly fine. And not all of the elements have IDs. Take this example:

$('.expandable').spindown();

<div class="expandable">First spindown</div>
<div>Content</div>
<ul>
    <li class="expandable">Second spindown</div>
    <img src="someimg.jpg"></img>
</ul>

Now, when a user clicks the div containing "First spindown", the div containing "Content" would slide down. And when a user clicks the li containing "Second spindown", the img element underneath it would slide down, kinda like an accordion.

This is because it finds the element with the "expandable" class and makes it clickable, toggling the slide effect for the next logical element.

Now, I have the code finding which elements were expanded, adding the elements inner text to an array, and saving that array to a cookie. Then when a user goes back to the page, it just expands the ones in that array stored in the cookie since that was the only way I could figure out how to save which elements were expanded in the cookie.

Upvotes: 0

Views: 765

Answers (1)

Elzo Valugi
Elzo Valugi

Reputation: 27866

There is a jQuery cookie plugin.

And a while ago I made some tests for a subcookie plugin. I hope you will find it useful, even is not a full grown plugin.

Upvotes: 0

Related Questions