I'm trying to save the Javascript Array object in a cookie. But as it didn't work properly, I found the stringify() function in json.js file. Following is my code:
$(".save").on('click', function(){
var lhb = [], csb = [], cons = [], vb = [],
txt = $("#accordion").find('textarea').serialize(),
lbs = $('.lh:checked').each(function(){ lhb.push($(this).val()); }),
cbs = $(".cs:checked").each(function(){ csb.push($(this).val()); }),
cs = $(".cons:checked").each(function(){ cons.push($(this).val()); });
vs = $(".vul:checked").each(function() { vb.push($(this).val()); });
scenario = {
"textArea" : txt
"lbs" : lbs,
"cbs" : cbs,
"cs" : cs,
"vs" : vs
},
cardId = $(this).parent().get(0).id;
save(cardId, scenario);
retreive(cardId);
});
Now in the save function I convert the scenario into JSON object using json.js
function save(cardId, formData){
$.cookie(cardId, JSON.stringify(formData), { expires: 7, path: '/' });
}
And this give me the following error:
Accessing selection direction on an input element that cannot have a selection.
Can anyone help me fix this error. Is that my approach is correct about saving the json array in to the cookie?
Thanks
$(".save").on('click', function(){
var lhb = [], csb = [], cons = [], vb = [],
txt = $("#accordion").find('textarea').val();
$('.lh:checked').each(function(){
lhb.push($(this).val());
});
$(".cs:checked").each(function(){
csb.push($(this).val());
});
$(".cons:checked").each(function(){
cons.push($(this).val());
});
$(".vul:checked").each(function() {
vb.push($(this).val());
});
var scenario = {
"textArea" : txt,
"lbs" : lhb,
"cbs" : csb,
"cs" : cons,
"vs" : vb
}
var cardId = $(this).parent().attr('id');
save(cardId, scenario);
retreive(cardId);
});
you have syntax error in scenario object, after txt there is no ',' also you didn't used correctly the serialize function, and wrong assignments in scenario objects
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With