I using jquery cookie plugin and here are my codes:
$('.Bookmark').click(function () {
var id = $(this).attr('rel');
if ($(this).hasClass('RedHeart')) {
$(this).removeClass('RedHeart');
$.removeCookie('Bookmarkid_' + id, id);
$(this).attr('title', 'Add');
} else {
$(this).addClass('RedHeart');
$.cookie('Bookmarkid_' + id, id, { expires: 3650 });
$(this).attr('title', 'remove');
}
});
$('.Bookmark').each(function () {
var id = $(this).attr('rel');
var $this = $(this);
if ($.cookie('Bookmarkid_' + id) == id) {
$this.addClass('RedHeart');
$this.attr('title', 'remove');
}
});
I'm wondering how can I find all cookies that set with a similar name. for example I set set 3 cookies with these names:
Bookmarkid_3132509
Bookmarkid_3432502
Bookmarkid_4433342
now I want to find and return all cookies that start with Bookmarkid_
name. I need something like this:
if ($.cookie().indexOf("Bookmarkid_") === 0) {
alert('yes')
}
Edit: it's not duplicate and not related to that topic, Still not solved my problem!!!
Read all available cookies:
$.cookie(); // => { "name": "value" }
So
Object.keys($.cookie()).forEach(function(cookieName) {
if (cookieName.indexOf("Bookmarkid_") === 0) {
alert('yes')
}
});
// next one return you only filtered cookies
Object.keys($.cookie()).reduce(function(collector, cookieName) {
if (cookieName.indexOf("Bookmarkid_") === 0) {
collector[cookieName] = $.cookie(cookieName);
}
return collector;
}, {});
// to filter cookies Names
Object.keys($.cookie()).filter(function(cookieName) {
return cookieName.indexOf('Bookmarkid_') === 0;
});