Javascript push Object to cookies using JSON
Posted
by
Hunkeone
on Stack Overflow
See other posts from Stack Overflow
or by Hunkeone
Published on 2013-11-07T20:42:25Z
Indexed on
2013/11/11
21:54 UTC
Read the original article
Hit count: 200
Hi All on click button I need to add object to array and then write array to cookies. From the start this array can be not empty so I parse cookie first.
function addToBasket(){
var basket = $.parseJSON($.cookie("basket"))
if (basket.length==0||!basket){
var basket=[];
basket.push(
{ 'number' : this.getAttribute('number'),
'type' : this.getAttribute('product') }
);
}
else{
basket.push(
{ 'number' : this.getAttribute('number'),
'type' : this.getAttribute('product') }
);
}
$.cookie("basket", JSON.stringify(basket));
}
And HTML
<button type="button" class="btn btn-success btn-lg" number="12" product="accs" onclick="addToBasket()">Add</button>
Unfortunately I'm getting Uncaught ReferenceError: addToBasket is not defined onclick. Can't understand what am I doing wrong? Thanks!
© Stack Overflow or respective owner