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.

9
  • Might you simply use the built-in method Array.includes instead? (it's less of a misnomer than inArray, and more closely matches what you're trying to do) Commented Mar 30, 2018 at 6:33
  • @CertainPerformance hello, yes I will try now, thanks Commented Mar 30, 2018 at 6:33
  • @CertainPerformance actually could you post a link? I only find inArray even when i look for include Commented Mar 30, 2018 at 6:34
  • For example if (lines.includes(curPostId)) Commented Mar 30, 2018 at 6:35
  • 1
    That's not a duplicate of what was suggested. This one is more about arrays than checking if a property is in localStorage, which is why I voted for reopening. Commented Mar 30, 2018 at 6:43

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

10 Comments

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.
localStorage.saveButton = $(this).attr('id'); shouldn't it be a push? Otherwise each time we land on a page this will be replaced
var saveButton = []; then on a click saveButton.push($(this).attr('id')); ?
actually the array should be set like i do for lines var saveButton = localStorage.getItem("saveButton") ? JSON.parse(localStorage.getItem("saveButton")) : []; correct?
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?
|
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)) {

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.