I have cookies on my site, so whenever the user clicks on 'bookmark', this will then add the class 'bookmarked' that will simply make this a different colour.
Could someone please show where I am going wrong? I want so whenever the user refreshes the page, it simply adds the class to the .bookmark class that they have previously clicked.
Also, I want that if they click the class 'bookmarked' this then removes the cookie for that. As they no longer want to book mark this.
HTML
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-cookie/2.1.3/js.cookie.js"></script>
<ul>
<li><div class="bookmark">Star</div></li>
<li><div class="bookmark">Star</div></li>
<li><div class="bookmark">Star</div></li>
</ul>
CSS
<style>.bookmarked {background:red;}</style>
JQuery
<script>
$(".bookmark").each(function () {
if (Cookies.get($(this))) {$(this).addClass('bookmarked');}
});
$('.bookmark').on('click', function (e) {
e.preventDefault();
$(this).toggleClass('bookmarked');
Cookies.set('bookmark-' + $('.bookmark').index(this), 'bookmarked', { expires: 365 });
});
</script>