Reputation: 2290
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
Upvotes: 0
Views: 367
Reputation: 36438
Start products
off as an array with a single object, not a bare object:
products = [ {"id":product_id} ];
Upvotes: 3