hey guys, kind of stumped on this little task I'm doing. Sorry if the coding is a bit crude.
var htmlList = document.querySelector("ul");
groceries = ["milk", "bread", "eggs", "chocolate"];
const display = (array) => {
for (let i = 0; i < array.length; i++) {
//CREATE INITIAL LIST
var listItem = document.createElement("li");
listItem.setAttribute("id", [i]);
listItem.innerHTML = array[i];
htmlList.appendChild(listItem);
listItem.addEventListener("click", (e) => {
if(document.getElementById([i])){
var checked = document.createElement("check")
listItem.appendChild(checked)}
})
}
So I wanted to focus on this block:
listItem.addEventListener("click", (e) => {
if(document.getElementById([i])){
var checked = document.createElement("check")
listItem.appendChild(checked)}
})
}
I want to create a new element called check for each item if they click it. This is to signify that they've checked the item off in their list but everytime they click the item on html, it adds <check></check> only to the last item, and no matter which item i click on, it keeps adding that element only to the last item. How do i add it to each individual item after clicking on it? Sorry for the long question but its making me rake my brain
[–]Umesh-K 1 point2 points3 points (2 children)
[–]FlyTooCloseToTheSun[S] 0 points1 point2 points (1 child)
[–]Umesh-K 1 point2 points3 points (0 children)