Detecting the presence of the selected
class instead of the checked state. With minimal alterations to the original code.
The event handler:
$('.share-actions li a').click(function(e) {
e.preventDefault();
$(this).toggleClass('selected');
setcookie();
});
And the general cookie get/set code:
var $checkboxes = $('.share-actions li a');
function setcookie() {
var options = $checkboxes.map(function() {
if ($(this).hasClass('selected')) return $(this).attr('id');
}).get().join(',');
$.cookie('new_cookie', options);
}
var alreadySetCookies = $.cookie('new_cookie').split(',');
alreadySetCookies.forEach(function(id) {
$("#" + id).addClass('selected');
});
Each "checkbox" will need a unique ID for this to work:
<ul class="share-actions">
<li><a href="#" id="facebook">Facebook</a></li>
<li><a href="#" id="twitter">Twitter</a></li>
<li><a href="#" id="instagram">Instagram</a></li>
</ul>