Hello Stefan,
Please find my code.
In this scenario I want the storage sync to hold an object with multiple array
So I have form on submit I want the array to be added to the storage object. But the code I am trying just gives me the last added form data. The previously added data disappear. Do let me know where I am going wrong.
let travel_logs = [];
const addtravel = (ev)=>{
ev.preventDefault();
let travel ={
current: document.getElementById("first_name").value,
destination: document.getElementById("last_name").value,
start_date : document.getElementById("start_date").value,
return_date : document.getElementById("return_date").value,
purpose : document.getElementById("purpose").value
}
travel_logs.push(travel);
// display purpose
var travellist=JSON.stringify(travel_logs);
chrome.storage.sync.set({'travellist': travellist}, function() {
});
}
document.addEventListener('DOMContentLoaded', ()=>{
document.getElementById('form_submit').addEventListener('click', addtravel);
document.getElementById("form_submit").addEventListener("click", myResetFunction);
});
document.body.onload = function() {
chrome.storage.sync.get({travellist: []}, function(items) {
if (!chrome.runtime.error) {
var travelids = JSON.parse(items.travellist);
jQuery.each(travelids, function(index, value) {
// display the data added to the form
$('.travel-rec').append(' <tr><td>'+travelids[0].current+'</td><td>'+travelids[0].destination+'</td><td>'+travelids[0].start_date+'</td><td>'+travelids[0].return_date+'</td><td>'+travelids[0].purpose+'</td> <td><a href="#"><i class="material-icons">more_vert</i></a></td></tr>');
});
}
});
}