I want to add some values to a container and save them in localstorage but when i click on the button to add them after the first time elements are being duplicated in the container not the localstorage. the weird thing is that when i reload the page the duplicates go out and the elements increament by the count of elements i added only here’s my code:
```
let divP = document.querySelector(".parent");
let input = document.querySelector("input");
let button = document.querySelector("button");
let container = document.getElementById("container");
let array = JSON.parse(localStorage.getItem("array")) || [];
let isUpdate = false;
function createElement() {
array.forEach((arr, id) => {
let div = document.createElement("div");
div.classList.add("div");
div.id = id;
div.innerHTML = arr.title;
container.appendChild(div);
});
}
createElement();
button.addEventListener("click", (e) => {
e.preventDefault();
let title = input.value.trim();
let id = Date.now();
if (title || id) {
let arrayInfo = { title, id };
if (!isUpdate) {
array.push(arrayInfo);
} else {
isUpdate = false;
}
localStorage.setItem("array", JSON.stringify(array));
createElement();
}
});