The map function also has an index value. Use that to create unique ID values.
const outputHtml = matches => {
if (matches.length > 0) {
const lookUpItem = matches.map((match, index) => `
<div class="foodInfo">
<h4 id="title">${match.foodDesc}</h4>
<small><span class="foodDetails">
<li>Protein: ${match.Protein} g</li>
<li>Calories: ${match.Calories} cals</li>
<li>Total Fat: ${match.Fat} g</li>
<li><button onclick="minus('amountNumber_${index}')" id="minus_${index}">-</button>
<span id="amount">Amount: <span id="amountNumber_${index}">${match.amount1}</span> / ${match.msreDesc1} </span>
<button onclick="add('amountNumber_${index}')" id="add_${index}">+</button> </li>
<button onclick="submit()" id="submit">Add to Diary</button>
</span></small>
</div>
`
).join('');
itemList.innerHTML = lookUpItem;
}
}
function add(el) {
amountNum = parseInt(document.getElementById(el)) + 1;
document.getElementById(el).innerText = amountNum;
console.log(amountNum.innerText)
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…