I currently have an array with three items, along with four buttons - each designed to add a new item to the array.
Although the process of adding items into the array using the buttons is successful, I am facing an issue with the display within the div element. The first item I add appears correctly in the div, but after that, whenever I click on any of the remaining buttons, the same item keeps showing up.
What could be causing this problem? Any suggestions?
var inventory = ["Apple", "Pear", "Orange"];
for (i = 0; i < inventory.length; i++)
document.getElementById("arrayDiv").innerHTML += inventory[i] + "<br>";
function addItem(newItem, itemBtn) {
inventory.push(newItem);
document.getElementById(itemBtn).style.display = "none";
console.log(inventory);
document.getElementById("arrayDiv").innerHTML += inventory[i] + "<br>";
}
<p>Fruits:</p>
<div id="arrayDiv"></div>
<hr>
<input id="banana" type="button" value="Banana" onclick="addItem('Banana', 'banana')">
<input id="melon" type="button" value="Melon" onclick="addItem('Melon', 'melon')">
<input id="pineapple" type="button" value="Pineapple" onclick="addItem('Pineapple', 'pineapple')">
<input id="coconut" type="button" value="Coconut" onclick="addItem('Coconut', 'coconut')">