mercredi 2 janvier 2019

Add only one list item upon pressing Enter key

Here's the JSFiddle link to my app: https://jsfiddle.net/kdowdell24/a9t7b23c/2/

The issue I'm having is with this block of code:

//Adds new item after pressing Enter key
let itemList = document.querySelectorAll("input[type=text]");
for (var i = 0; i < itemList.length; i++) {
    addEventListener("keypress", function(e) {
        if (e.key === "Enter") {
            createNewItem();
            console.log("New item created!");
        }
    })
}

What's happening is that when I press Enter while in the textbox of a list item it adds multiple entries instead of just one. What I'm trying to accomplish is to have only one item entry created below the current one after hitting Enter.

Aucun commentaire:

Enregistrer un commentaire