I am trying to make a shopping list where I can increment or decrement the quantity for each item with up and down buttons.
My issue is that when I click the buttons, instead of updating the quantity of the selected <li>
element, only the input in the first <li>
element is changed.
Here is my code at the moment:
const listSelect = document.getElementById("liste")
function addItem() {
const pdtValue = document.getElementById("pdt-input").value;
const newLi = document.createElement('li');
newLi.setAttribute('id', 'myLi');
const checkbox = document.createElement('input');
checkbox.setAttribute("type", "checkbox");
checkbox.setAttribute("id", "check");
checkbox.onclick = updateItem.bind(checkbox);
const trash = document.createElement('button');
trash.setAttribute("id", "delete");
const icon = document.createElement('i');
icon.setAttribute('class', 'fas fa-trash-alt');
icon.setAttribute('style', 'pointer-events: none');
const upButton = document.createElement('button');
upButton.setAttribute('id', 'up');
const upIcon = document.createElement('i');
upIcon.setAttribute('class', 'fas fa-arrow-right');
upIcon.setAttribute('style', 'pointer-events: none');
const downButton = document.createElement('button');
downButton.setAttribute('id', 'down');
downButton.setAttribute('onClick', 'down()')
const downIcon = document.createElement('i');
downIcon.setAttribute('class', 'fas fa-arrow-left');
downIcon.setAttribute('style', 'pointer-events: none');
const Qty = document.createElement('input');
Qty.setAttribute("type", "text");
Qty.setAttribute("id", "quantity");
Qty.setAttribute("value", "1");
const text = document.createTextNode(pdtValue);
if (pdtValue !== "") {
newLi.appendChild(text);
newLi.appendChild(downButton);
downButton.appendChild(downIcon);
newLi.appendChild(Qty);
newLi.appendChild(upButton);
upButton.appendChild(upIcon);
newLi.appendChild(checkbox);
newLi.appendChild(trash);
trash.appendChild(icon);
listSelect.appendChild(newLi)
}
newLi.addEventListener('click', function(e) {
if (e.target && e.target.id == "delete") {
e.target.parentNode.remove();
}
});
newLi.addEventListener('click', function(e) {
if (e.target && e.target.id == "up") {
var quantité = document.getElementById('quantity');
quantité.value++
}
});
newLi.addEventListener('click', function(e) {
if (e.target && e.target.id == "down") {
var quantité = document.getElementById('quantity');
quantité.value--
}
});
function updateItem() {
if (this.checked) {
this.parentNode.style.textDecoration = "line-through";
} else {
this.parentNode.style.textDecoration = "none";
}
}
};
body {
display: flex;
flex-direction: column;
}
h1 {
display: flex;
align-self: center;
}
ul {
display: flex;
flex-direction: column;
justify-self: center;
background-color: #faffba;
background-image: url("https://www.transparenttextures.com/patterns/beige-paper.png");
min-height: 60px;
}
li {
display: flex;
flex-direction: row;
width: 80vw;
}
button {
text-decoration: none;
}
#check {
margin-left: auto;
}
#addInputs {
display: flex;
align-self: center;
}
#quantity {
width: 20px;
margin-right: 5px;
margin-left: 5px;
text-align: center;
}
#up {
border-radius: 50%;
}
#down {
margin-left: 5px;
border-radius: 50%;
}
<body>
<h1>Grocery List</h1>
<ul id="liste">
</ul>
<div id="addInputs">
<input placeholder="Produit" id="pdt-input"></input>
<!--<input placeholder="Qty" id="qt-input"></input>-->
<button class="add-btn" onClick="addItem()" value='Submit'>Add</button>
</div>
<div>
<button onClick="customList()">
Favs
</button>
</div>
</body>