0

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.

rob.m
  • 9,843
  • 19
  • 73
  • 162

2 Answers2

2

Just use the Array.includes method instead - it's less confusing, more appropriately matches what you're looking for, and doesn't require jQuery.

if (lines.includes(curPostId)) {
  // ...

Also note that you can simplify your syntax by assigning and getting from localStorage's dot properties directly, for example:

var lines = JSON.parse(localStorage.lines || '[]');
// ... 
localStorage.saveButton = $(this).attr('id');
// ... 
localStorage.lines = JSON.stringify(lines);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • I just copied that from your line `localStorage.setItem('saveButton', $(this).attr('id'));`, which you can replace with dot notation if you want to make your code easier to read with less syntax noise. – CertainPerformance Mar 30 '18 at 06:51
  • `localStorage.saveButton = $(this).attr('id');` shouldn't it be a push? Otherwise each time we land on a page this will be replaced – rob.m Mar 30 '18 at 06:51
  • `var saveButton = [];` then on a click `saveButton.push($(this).attr('id'));` ? – rob.m Mar 30 '18 at 06:53
  • actually the array should be set like i do for lines `var saveButton = localStorage.getItem("saveButton") ? JSON.parse(localStorage.getItem("saveButton")) : [];` correct? – rob.m Mar 30 '18 at 06:54
  • I didn't look at what the code was doing there, I just copied it and converted it into dot notation. But it looks like `saveButton` is intended to be a string, not an array? – CertainPerformance Mar 30 '18 at 06:59
  • actually i probably don't even need saveButton, based on the code in the question i do `var lines = localStorage.getItem("lines") ? JSON.parse(localStorage.getItem("lines")) : [];` and I save the ids on a click and push them into it and i get an array like `[242, 248`], so i suppose by simply doing `if (lines.includes(curPostId)) { $('#'+curPostId).attr('disabled', true); }` would be fine? – rob.m Mar 30 '18 at 07:02
  • matter of fact if I do `if (lines.includes(curPostId)) { console.log("hello"); $('#'+curPostId).attr('disabled', true); }` I get `hello` yet the `attribute` is not set, is `$('#'+curPostId).attr('disabled', true);` correct ? – rob.m Mar 30 '18 at 07:05
  • mm nope, i should be getting the matching value as use that for `$('#'+ID).` so how do I grab the matching value? – rob.m Mar 30 '18 at 07:08
  • I was only describing a better array method (and a better way of using localStorage), I have no idea what you're actually trying to accomplish here. If you have another question feel free to post it, but comments aren't the right place for that. – CertainPerformance Mar 30 '18 at 07:10
  • I am trying to resolve this answer, because the given solution works but only with a single id, but each time i land on page i add an id, https://stackoverflow.com/a/49561288/1018804 – rob.m Mar 30 '18 at 07:14
1

$.inArray return index of element so what happens is, your element is at '0' th index and if condition will be false if value is 0

So you can use

 if($.inArray(curPostId, lines) !== -1) {

or Use includes method of ES6

if (lines.includes(curPostId)) {
Kiran Shinde
  • 5,732
  • 4
  • 24
  • 41
  • yup, this works, had to accept the other answer as it was firstly suggested on a comment. But the principle is the same and this answer is correct too. Thanks – rob.m Mar 30 '18 at 06:47