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