This morning I posted and @Victor solved my problem, but now I have another problem and I haven't been able to solve it since noon.
Link: Show the element saved in localStorage when reload the page
I improved the code by adding the add and remove button. The add add button worked but the remove button doesn't work. Where is my mistake?
<ul id="gfg">
<li>Computer Network</li>
<li>Data Structures using C</li>
</ul>
<input type="text" id="text1">
<input type="submit" onclick="test()" value="add"></input>
<input type="submit" onclick="remove()" value="remove"></input>
function getStorageItems() {
var data = localStorage.getItem('items');
return data ? JSON.parse(data) : null;
}
function saveStorageItem(text) {
var array = getStorageItems() || [];
array.push(text);
localStorage.setItem('items', JSON.stringify(array));
}
function addItem(text, save) {
var node = document.createElement('li');
node.textContent = text;
document.getElementById('gfg').appendChild(node);
if (save !== false)
saveStorageItem(text);
}
function test() {
a = document.getElementById('texto1').value;
addItem(a);
}
function remove() {
a = document.getElementById('texto1').value;
localStorage.removeItem(a);
}
function loadFromStorage() {
var array = getStorageItems();
if (array) {
for (var i = 0; i < array.length; i++)
addItem(array[i], false);
}
}
loadFromStorage();
I want to do this: [enter image description here][1] [1]: https://i.stack.imgur.com/DVOYf.png