0

Currently I have this multiple button with data-id and data-name

Here's my idea and sample code

$(".clickCompare").click(function ({
      var id = $(this).attr('data-id');
      var name = $(this).attr('data-name');           

      var datas = [id , name];
      localStorage["datas"] = JSON.stringify(datas);
      var stored_datas = JSON.parse(localStorage["datas"]);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<button type="button" class="clickCompare btn btn-primary" data-id="1" data-name="Sample1">Compare</button>
<button type="button" class="clickCompare btn btn-primary" data-id="2" data-name="Sample2">Compare</button>
<button type="button" class="clickCompare btn btn-primary" data-id="3" data-name="Sample3">Compare</button>

What I'm trying to do is to store the data-id and data-name on variable datas every click of clickCompare class. So that I can call them whenever I want.

And if the ID already exists it will not store on variable datas

1
  • Is there an error? Commented Dec 9, 2020 at 3:47

2 Answers 2

1

I think what you want to do is to save an array of objects (tuple). In this case datas should be something like

let datas = [{id: 1, name: 'first'}, {id: 2, name: 'second'}];

so each time you click you can add a new item in the array. To do that, you can check if there's an item in the array with that id. Use a function like this

$(".clickCompare").click(() =>{
    let id = $(this).attr('data-id');
    let name = $(this).attr('data-name');  
    let datas = [];

    if(localStorage.key('datas')){
        datas = JSON.parse(localStorage.getItem('datas'));
    }

    if(!datas.filter(x => x.id == id)){
        datas.push({id: id, name: name});
    }

    localStorage.setItem('datas', JSON.stringify(datas));
    console.log(localStorage.getItem('datas')); //to remvoe after testing
});
Sign up to request clarification or add additional context in comments.

Comments

0

Create key value pair instead of array as below -

$(".clickCompare").click(function() {
  var id = $(this).attr('data-id');
  var stored_datas = localStorage["datas"];
  if (!stored_datas) {
    stored_datas = "{}";
  }
  var datas = JSON.parse(stored_datas);
  var existingData = datas[id];
  if (existingData) {
    console.log(`Data for id ${id} already exists - ${existingData}`);
    return;
  }
  var name = $(this).attr('data-name');
  datas[id] = name;
  var json = JSON.stringify(datas);
  localStorage["datas"] = json;
  console.log(`Added new data for id ${id}  - ${name}`);
  var stored_datas = JSON.parse(localStorage["datas"]);
  console.log(`Local Storage content - ${json}`);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<button type="button" class="clickCompare btn btn-primary" data-id="1" data-name="Sample1">Compare</button>
<button type="button" class="clickCompare btn btn-primary" data-id="2" data-name="Sample2">Compare</button>
<button type="button" class="clickCompare btn btn-primary" data-id="3" data-name="Sample3">Compare</button>

Since stackoverflow disables localStorage for snippets, refer link for demo

2 Comments

the output should be something like this {id: 1, name: 'first'}, {id: 2, name: 'second'} right
No it will store it like - {"1": "Sample1","2":"Sample2","3":"Sample3"}, but you can also do it the way you mentioned, by changing some code.

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.