$lis = document.getElementsByTagName('li');
for (let i = 0; i < $lis.length; i++) {
$li = $lis[i];
$span = $li.getElementsByTagName('span')[0];
$button = $li.getElementsByTagName('button')[0];
$button.addEventListener('click', $li.remove);
}
<ul>
<li>
<span>1</span>
<button>x</button>
</li>
</ul>
On the last line I'm using Element.remove() method, but instead of deleting the <li> it deletes <button>.
Explain to me, please, why. I feel that there is something with this happening, but I want to understand this completely not just feel.
P.S. I also understand that I can solve this by wrapping the $li.remove in an anonymous function, but the point of my question is to understand described behaviour not just to solve the problem. Thanks.