If you need an answer without json2 (I mean that not everyone knows json2), you can use this answer; (And if you like it, maybe you could rate it: D)
<script type="text/javascript">
function uniqueArr(a) {
temp = new Array();
for(i=0;i<a.length;i++){
if(!contains(temp, a[i])){
temp.length+=1;
temp[temp.length-1]=a[i];
}
}
return temp;
}
function contains(a, e) {
for(j=0;j<a.length;j++)if(a[j]==e)return true;
return false;
}
jQuery(document).ready(function($){
$("input[type=checkbox]").each(function () {
$(this).change(updateCount);
});
updateCount();
function updateCount () {
var val;
var my_cookie="";
var new_my_cookie="";
var cookie_array;
var new_cookie_array;
var new_cookie_string="";
var number=0;
var temp_cookie="";
my_cookie=$.cookie("chosen_ones");
$(':checkbox:checked').each(function(){
val= $(this).val();
if((val!=null)&&(val!="")){
my_cookie=val+"|"+my_cookie;
}
});
new_cookie_array=uniqueArr(my_cookie.split("|"));
$.each(new_cookie_array, function(index, values) {
if((values!="")&&(values!="null")&&(values!=null)){
temp_cookie=values+"|"+temp_cookie;
}
});
$.cookie("chosen_ones", null);
$.cookie("chosen_ones", temp_cookie);
var cookie_array=$.cookie("chosen_ones").split("|");
$(':checkbox:not(:checked)').each(function(){
val= $(this).val();
$.each(cookie_array, function(index, values) {
if((values==val)&&(values!=null)&&(values!="")&&(values!="null")&&(values!="")){
cookie_array[index]="";
}
});
});
new_cookie_array=uniqueArr(cookie_array);
$.each(new_cookie_array, function(index, values) {
if((values!="")&&(values!=null)&&(values!="null")){
new_cookie_string=new_cookie_string+"|"+values;
}
});
$.cookie("chosen_ones", null);
$.cookie("chosen_ones", new_cookie_string);
alert($.cookie("chosen_ones"));
var temping_string=$.cookie("chosen_ones");
$("#count").text(temping_string.split("|").length-1);
$("#status").toggle(temping_string.split("|").length-1 >= 0);
};
});
</script>
source
share