Click here for a working example
To check if a div is visible, you can use the is(":visible")
function:
if ( $("#mainleft-content").is(":visible") ){
alert('The div is visible');
}
else{
alert('The div is hidden');
}
If you need to work with cookies, you can implement the following functions:
function setCookie(cookieName, cookieValue, expirationDays) {
var expirationDate = new Date();
expirationDate.setDate(expirationDate.getDate() + expirationDays);
var formattedValue = escape(cookieValue) + ((expirationDays == null) ? "" : "; expires=" + expirationDate.toUTCString());
document.cookie = cookieName + "=" + formattedValue;
}
function getCookie(cookieName) {
var i, x, y, cookieArray = document.cookie.split(";");
for (i = 0; i < cookieArray.length; i++) {
x = cookieArray[i].substr(0, cookieArray[i].indexOf("="));
y = cookieArray[i].substr(cookieArray[i].indexOf("=") + 1);
x = x.replace(/^\s+|\s+$/g, "");
if (x == cookieName) {
return unescape(y);
}
}
}
You can then set the cookie using the following code:
$(document).ready(function () {
$("#expand-hidden").click(function () {
$("#mainleft-content").toggle();
SetCookie("DivStateVisible", $("#mainleft-content").is(":visible"),5);
});
});
For managing cookies in jQuery, consider using the jQuery Cookie plugin available at this link:
function setCookie(cookieName, cookieValue, expirationDays) {
$.cookie(cookieName, cookieValue, { expires : expirationDays });
}
function getCookie(cookieName) {
return $.cookie(cookieName);
}