jQuery cookie with JSON “undefined is not a function”
What am i doing wrong here ?
function add_to_compare(product_id){
products = getCookie("compare_list");
if(typeof products != "undefined" && products){
products = jQuery.cookie( 'compare_list' );
products = jQuery.parseJSON(products);
console.log(products);
new_product = {"id":product_id};
products.push(new_product);
}else{
products = {"id":product_id};
jQuery.cookie("compare_list",JSON.stringify(products));
}
return false;
}
On the first call its ok , the cookie is set and after parsing it shows like that on my console:
Object {id: 72}
But on the second click I cannot push another item
Getting “undefined is not a function” on products.push
Start products
off as an array with a single object, not a bare object:
products = [ {"id":product_id} ];
Read more here: Source link