I have an array saved in localstorage and trying to delete an element onclick based on input value. I have the code below but I cant make it work. Please help, Thanks in advance :)
var datarray = [];
function deldata() {
// retrieve stored data (JSON stringified) and convert
var storedData = localStorage.getItem("list_data_key");
if (storedData) {
datarray = JSON.parse(storedData);
}
var titleValue = document.getElementById('listxt').value;
// Find and remove item from an array
var i = array.indexOf(titleValue);
if(i != -1) {
array.splice(i, 1);
}
localStorage.setItem("list_data_key", JSON.stringify(datarray));
datapost();
}
deldata...? And you're usingJSON.stringify(datarray)but changingdata. Please rethink your question and try again