If there is an item already, and the same item is added again then it should show "2 now" beside that grocery item. This number should increase every time the same item is added to the grocery list. Please help me solve this issue. This is my code:
const groceries = document.getElementsByClassName("groceries")[0];
const deleteall=document.getElementById("deleteall");
const allitems=document.getElementById("allitems");
const userinput=document.getElementById("userinput");
deleteall.addEventListener("click",function(){
allitems.innerHTML="";
})
userinput.addEventListener("keydown",function(event){
if(event.key == "Enter")
additem();
})
function additem(){
var h2= document.createElement("h2");
h2.innerHTML="-"+ userinput.value;
if (userinput.value == "") {
alert("Enter some item");
return false;
}
h2.addEventListener("click",function(){
if (h2.style.color === "black") {
h2.style.color = "red";
} else {
h2.style.color = "black";
}
})
allitems.insertAdjacentElement("beforeend",h2);
userinput.value="";
}