When I click li links, I would like to get index value using this variable. How I can achieve it?
let liList = document.getElementsByTagName("li");
for (let i = 0; i < liList.length; i++) {
liList[i].onclick = function () {
alert(liList.indexOf.call(this)); //I want to use this to get i value
};
}
<ul>
<li>11</li>
<li>22</li>
<li>33</li>
</ul>
liList.indexOf(this)? Though theiisletscoped, so not sure why you couldn't just use it. Or use aforEachwith a closure to get the index and not need this logic.