0

I am trying to put element ID's into an array upon clicking the element, and store this array in local storage. Then call this array, and push more element ID's every time an element is clicked. It is turning out to be trickier for me, than I thought it would be.

I am using javascript/jquery.

My code:

var reportArray = []
var storedArray = JSON.parse(localStorage.getItem("reportarray"));
if(storedArray == '') {
    reportArray.push("foo");
    localStorage.setItem("reportarray", JSON.stringify(reportArray));
} else {
    reportArray.push(storedArray);
    localStorage.setItem("reportarray", JSON.stringify(storedArray));
}

I realize this code is probably way off (it doesn't even work correctly), but I am at least giving it a try. Help please?

6
  • Post an working example including HTML and JavaScript snippet Commented Jan 22, 2016 at 8:33
  • Thats the problem. I am unsure how to make it work. That is why I am asking for help. Commented Jan 22, 2016 at 8:34
  • It's not clear what is i3 and also the functions GetC and SetC Commented Jan 22, 2016 at 8:35
  • can you share GetC and SetC? Commented Jan 22, 2016 at 8:35
  • Yes, let me edit the code real quick to clarify Commented Jan 22, 2016 at 8:35

4 Answers 4

2

It doesn't look that far off. The main issue I see is that in the else clause, if the storedArray already exists, then you don't push any new elements to it. It might also be safer to check reportarray for null before attempting to parse it.

Sign up to request clarification or add additional context in comments.

2 Comments

My thoughts were that if it does exist, then I may want to "update" it with new element ID's... Would this not mean that I would need to push new elements to it? I am willing to admit that I could be thinking about this the wrong way.
You said that every time an element is clicked, you want to push an element ID to the array. That's what this code isn't doing. The line after the else should read something like storedArray.push("bar");
1

API:

function storeId(id) {
    var ids = JSON.parse(localStorage.getItem('reportArray')) || [];

    if (ids.indexOf(id) === -1) {
        ids.push(id);
        localStorage.setItem('reportArray', JSON.stringify(ids));
    }

    return id;
}

The function takes an ID to store in the local storage. If the local storage item is not yet present, it creates a fresh new array otherwise uses the array from the local storage. Also, makes sure the id does not already exist while pushing it to the storage.

USAGE:

$(function () {
    $('.btn').click(function () {
        var id = $(this).attr('id');
        storeId(id);
    });
});

EXAMPLE

3 Comments

I tried to implement this, but I get an error in the javascript console: Uncaught SyntaxError: Unexpected end of input
Check out the example fiddle. It works there. May be a syntax error on your implementation.
Your fiddle works, but for whatever reason, I am totally unable to get your exact code working in my script. I had to use el_tamatoe's code to get it to work. Thank you for your help! It was so invaluable.
0

If you need to put an ID inside the local storage, this could be the strategy:

var reportArray = localStorage.getItem("reportarray");
if(reportArray != null && reportArray != '') {
    var storedArray = JSON.parse(reportArray);
    storedArray.push("foo");
    localStorage.setItem("reportarray", JSON.stringify(storedArray));
} else {
    var storedArray = ["foo"];
    localStorage.setItem("reportarray", JSON.stringify(storedArray));
}

Obviously you need to put the ID instead of "foo".

1 Comment

Ended up using this exact code, except for replacing foo with the id to get it working. Thank you!
0

I would say something like this :

HTML :

<button id="elt1">Elt 1</button>
<button id="elt2">Elt 2</button>
<button id="elt3">Elt 3</button>
<button id="elt4">Elt 4</button>
<button id="elt5">Elt 5</button>

JS :

$(document).ready(function(){
  $('button').click(function(){
    var id = this.id;
    var reportArray = JSON.parse(localStorage.getItem("reportarray")) || [];
    if (!~reportArray.indexOf(id)) reportArray.push(this.id);
    localStorage.setItem("reportarray", JSON.stringify(reportArray));
  });
});

JS (push id even if already existing) :

$(document).ready(function(){
  $('button').click(function(){
    var id = this.id;
    var reportArray = JSON.parse(localStorage.getItem("reportarray")) || [];
    reportArray.push(this.id);
    localStorage.setItem("reportarray", JSON.stringify(reportArray));
  });
});

4 Comments

This one works... except for it only saves one id, not multiple id's in an array. Seems so close!
Ok. Then just remove if (!~reportArray.indexOf(id)) It was to make sure every id is pushed only once
It still didn't seem to want to work for me. Probably my fault. Thank you so much!
Really ? That's weird ... Mine works perfectly fine : jsfiddle.net/OxyDesign/yvtkwr4t (I'm trying with Chrome)

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.