I have an object that Im storing in a cookie, then getting the object and using it for some checks, that’s good and all but the problem Im having is with the line
if(!arr.hasOwnProperty($(this).val())){
in
$(".not").click(function() {
//alert($(this).val()+JSON.stringify(arr));
if(!arr.hasOwnProperty($(this).val())){
$.getJSON("<?php echo BASE_URL;?>json/startup/not", {id: $(this).val()}, function(json){
if(json.status == "OK"){
arr[json.data.id] = json.data.id;
arr = JSON.stringify(arr);
$.cookie('votedId', arr);
$('#percent'+json.data.id).html(json.data.percent);
$('#votes'+json.data.id).html(json.data.votes);
}
});
}else{
alert("Already voted on this startup!");
}
//alert($(this).val());
});
What happens is that for the first time I do it, it realized that it has no property by the value being passed in, and lets the vote happen. Then it doesn’t let it vote, as it should.
The problem however, is on other elements on the page that they can vote then say that there is a property by the passed in id, even though it’s not there, after the first element is voted on. It lets me vote if I refresh the page though.
Just because I feel the above is a little hard to grasp from that explanation, let me give an example.
I have multiple things with a vote hot or vote not. The first one’s id is 1, the second’s is 2, and so on. I vote that 1 is hot, so the cookie and arr array get updated to have a property 1, so it can’t be voted on again. However, after that, all other things say they’ve been voted on too, even though they haven’t been. So now I can’t vote on 2. But if I refresh the page it’s fine. But then Id have to refresh the page if I wanted to vote on element 3.
Any ideas?
In your code, you have:
The second line above overwrites your
arrvariable into a string. Second time, it is string object on which you are calling.hasOwnProperty(...)– This will result in a JS error.This should solve it: