Sorry to bother you, but I've been figuring it out for two hours. The problem is that my code adds only one element, but does not add other ids, writes "already have"
$(function(){
$(".js-compare-add").click(function(){
var item_id = $(this).data('id');
if(Cookies.get('compare_id_list')){
var compareListStr = Cookies.get('compare_id_list');
var compareListArr = compareListStr ? compareListStr.split(',') : [];
if (compareListArr.indexOf(item_id) > -1) {
compareListArr.push(item_id);
Cookies.set('compare_id_list', compareListArr.join(','), {expires: 10});
alert('Save! ' + item_id);
} else {
alert('already have ' + item_id);
}
} else {
alert('There was no list, we created it and added this position ' + item_id);
Cookies.set('compare_id_list', item_id);
}
});
});
Just in case - I used it for cookies https://github.com/js-cookie/js-cookie if (jQuery.inArray(item_id, compareListArr) === -1) didn't help either