Preserving links and creating default state with cookies
Hello everyone,
I was assisted with this solution which adds a class to a clicked link so that it becomes the activelink. The style is than preserved using Cookie even when the user refreshes the browser.
My question is: How can I modify this code so that when the page loads a specified link or tab is assigned the activeLink class so that it becomes the default? However, if other links are clicked; they than become active addClass/removeClass.
Finally if a user returns to the page then their previous selection is still preserved, the default will only go to the first tab if the user had no cookies previously set; or never visited the page before.
Here is the working code:
- $(document).ready(function() {
var idActive = $.cookie('cookieActive');
if (idActive) {
$('#'+idActive).addClass('activeLink');
}
$('.resizer li a').click(function() {
var id = $(this).attr('id');
$(".resizer li a").removeClass("activeLink");
$(this).addClass("activeLink");
$.cookie('cookieActive', id); //Save anchor id as cookie
$.cookie('cookieActive', id, { expires: 10000});
});
});
Thanks everyone!
AR