I’m trying to replicate a UI effect as on http://mcfc.co.uk I have written a script that hides a div on click function and applies a class to a div with the #id corresponding to the div that was clicked, and the reverse. I’m new to jQuery, how would I save the state of these ‘clicked’ divs to a cookie to show which were hidden etc??
Thanks for any help.
<script type="text/javascript">
$(document).ready(function(){
$('.portlet').click( function(){
var idtext = this.id;
$(this).hide();
$("[id*=" + idtext + "]").not(this).addClass('add');
});
$("#content-footer div").click( function(){
var idtext = this.id;
$(this).removeClass('add');
$("[id*=" + idtext + "]").not(this).show();
});
})
</script>
HTML:
DIVs that are hidden/shown on click…..
<div class="column" id="col0">
<div class="portlet" id="p_0">
<div class="portlet-header">Feeds</div>
<div class="portlet-content">Lorem ipsum dolor sit amet, consectetuer adipiscing elit
</div>
</div>
<div class="portlet" id="p_1">
<div class="portlet-header">News</div>
<div class="portlet-content">Lorem ipsum dolor sit amet, consectetuer adipiscing elit
</div>
</div>
</div>
etc....
DIV’s that class is applied to…..
<div id="content-footer">
<div id="p_0"></div>
<div id="p_1"></div>
<div id="p_2"></div>
<div id="p_3"></div>
<div id="p_4"></div>
</div>
I posted a demo of what I think you want here. Be sure to include the jQuery cookie plugin.
Here is the HTML I used:
And the script:
Update: Added “indexOf” prototype at the end of the script above to fix an IE bug
Update #2: Added cookieExpires variable, use a number to set the number of days, set it as a date() to set an end date or “null” to set it as a session cookie.