I've tried to
<ul class="product">
<li>aaaaa</li>
<li>bbbbb</li>
<li class="on">ccccc</li>
<li>ddddd</li>
</ul>
const product = document.querySelector(".product");
const productList = product.querySelectorAll("li");
function getIdx() {
productList.forEach((list, idx) => {
let productClassList = list.classList;
let parsedList = Array.from(productClassList);
if (parsedList.includes("on")) {
return idx;
}
});
};
function printIdx() {
let idx = getIdx();
console.log(idx); // Print undefined
}
productList.forEach(list => {
list.addEventListener("click", printIdx);
});
but it is not working, it is always print "undefined" when i click content of li tag
i wanna get index when i click content of li tag and i wanna use that like web shopping site
how can i get index?
parsedList.includes("on")- alwats returns false..forEach((list, idx) => { }, not for the functiongetIdx()for(let idx = 0; idx < productList.length; idx++) { ... }onelement or index of the clicked element?