Based on this answer I should be using $.inArray
, therefore I do:
var curPostId = $(".my_post_id").attr("data-id");
if($.inArray(curPostId, lines)) {
$('#'+localStorage.getItem('saveButton')).attr('disabled', true);
}
If I do: console.log(curPostId); I get 248
which is correct. Then if I do console.log(lines);
I get [242, 248]
Lines is defined like this:
var lines = localStorage.getItem("lines") ? JSON.parse(localStorage.getItem("lines")) : [];
But the check if it's in Array doesn't happen as this it's not applied $('#'+localStorage.getItem('saveButton')).attr('disabled', true);
This is how I set daveButton on local storage
$(".save_post").on("click", function() {
if (counter >= 2) {
alert('nope');
return;
} else {
$(this).attr("disabled", "true");
localStorage.setItem('saveButton', $(this).attr('id'));
var thisId = $(".my_post_id").attr("data-id");
lines.push(thisId);
localStorage.setItem("lines", JSON.stringify(lines));
}
});
This question is a follow up to my previous question how to keep button state across different pages which has an answer that works but only partly.