views:

47

answers:

2

Hello,

How to use jquery cookies in showing/hiding elements in a page ? I got the plugin from here

Tried some method but i am not successful. I used slideUp() and slideDown() functions to show/hide elements.

When a element is slided up a cookie should be set. when the page is refreshed, the element should be in slided up position

How to apply cookie when slided up and how to get the cookie when page is refreshed ? I need help !

+1  A: 

I think the following is what you're looking for. Let me know if I am mistaken. Good luck!

http://jsfiddle.net/8VCJY/8/

- EDIT -

Sorry! My cookies were getting set backwards. It does work now (new link). Again, all apologies.

- EDIT -

Actually, I don't even know if it does work. It looked like it did, but then reviewing my code, I can't see how that could be right. All in all, I'm pretty sure that the setting of the cookie at the appropriate time and whatnot will at least get you on the right path. I think my usage of the plugin is wrong, to be honest (I've never used it before), but I gave it what I could. I hope it was at least somewhat useful to you.

Lance May
@Lance May thanks for the try. the cookie is stored, but the cookie is not got. it remains open even it is closed before
Aakash Chakravarthy
A: 

Would this do the trick?

<!DOCTYPE html>
<html> <!-- xmlns="http://www.w3.org/1999/xhtml" lang="en" -->
<head>
    <style type="text/css">
    #outer {height:500px;width:500px;background-color:black;position:relative;}
    #slide-me {height:50px;width:50px;background-color:white;position:absolute;top:250px;left:5px;}
    </style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
<script type="text/javascript" src="jquery.cookie.js"></script>
</head>

<body>
<div id="outer">
    <div id="slide-me"></div>
</div>
<button type="button" id="myBtn">Slide it!</button>
<script type="text/javascript">
$slideObj = $('#slide-me');
var topOffset = $.cookie('myCookie');
if(topOffset)
    $slideObj.offset({top: topOffset - $slideObj.height(), left:$slideObj.offset().left});
$('#myBtn').click(function() {
    $slideObj.slideUp();
    $.cookie('myCookie', $slideObj.offset().top);
});
</script>
</body>
</html>

P.S I'm using the Cookie plugin.

ifaour