I am trying to use Cookie so that a default style OR a specific style is applied in reference to the anchor tag clicked, even when the browser is closed/reopen.
So if the user clicked the second link, close or refresh the browser and reopen, than the style should still be active, if it is their first time the default should apply.
This is a little over my turf.
Here is the HTML:
<a id="default" href="#/">Default Style</a>
<a id="style2" href="#/">Style 2</a>
<a id="style3" href="#/">Style 3</a>
<ol>
<li><span>Hello World</span></li>
</ol>
JQuery: (Compliments of StackOverflow)
<script type="text/javascript">
$('#default').on('click',function(){
$('ol li span').removeClass(function() {
return $(this).attr('class');
}).addClass('default');
});
$('#style2').click(function(){
$('ol li span').removeClass(function() {
return $(this).attr('class');
}).addClass('style2');
});
$('#style3').click(function(){
$('ol li span').removeClass(function() {
return $(this).attr('class');
}).addClass('style3');
});
</script>
CSS:
<style type="text/css">
.default{
color: #333;
}
.style2{
color: #999;
}
.style3{
color: #800;
}
</style>
There is a cookie plugin for jQuery you can use to set and get cookies, but if browsers older than IE8 are’nt an issue, I’d go for local storage instead and just use the default style for the 1% of visitors using older browsers.
If you add a class to your links, you can target all of them in one function, and just set the class based on the ID clicked, here’s an example using local storage:
FIDDLE