This question is not a duplicate, as it affects not only closures, but also other common errors in JS, as well as their elimination using the new features of the ES 2015 specification.
When iterating through elements, it is not possible to register correctly the click event handlers of the iterated elements. Events of all enumerated elements are registered by an anonymous function intended for the most recent element.
As planned. 1 When you hover over a fieldset message, the delete icon of each message is highlighted, and when you move the mouse, the delete icon of the corresponding message disappears again. 2 Hovering over the delete icon pops up a prompt with the time of the corresponding message. 3 When you click on the icon of the corresponding delete message, an alert over time of the corresponding message pops up.
I have 1 When hovering over any fieldset message, the delete icon of the last message is highlighted, while moving the mouse, the delete icon, the last message, disappears again. 2 This is the only thing that works correctly. 3 When you click on the icon to delete any message, an alert with the last message time pops up.
What am I doing wrong that I do not understand here and how can I do it right?
window.addEventListener('load',function() { var imgs=document.getElementsByClassName('right')[0].getElementsByClassName('del'); for(var i=0,l=imgs.length;i<l;i++) { var img=imgs[i]; var doc=document.getElementById('mes-'+img.dataset.date+'-'+img.dataset.fid); doc.addEventListener('mouseover',function(){img.style.opacity=1;}); doc.addEventListener('mouseout',function(){img.removeAttribute('style');}); img.setAttribute('title','Delete '+img.dataset.title+' message'); img.addEventListener('click',function() { if(!isNaN(img.dataset.date)&&img.dataset.date>0&&!isNaN(img.dataset.fid)&&img.dataset.fid>0&&img.dataset.title) alert('Are you sure you want to delete '+img.dataset.title+' message?'); }); } }); * { margin:0; padding:0; border:0; border-radius:5px; transition:all 0.2s linear; } html,body { height:100%; min-height:100%; } body { background-color:#fff; color:#000; font:12px/18px Arial,Helvetica,sans-serif; margin:0 auto; } fieldset { border:1px solid #ccc; } legend { font-weight:bold; } div.message { min-width:400px; width:400px; max-height:400px; margin:0 auto; padding:10px; text-align:center; } div.message div.left, div.message div.right { float:left; height:100%; overflow:auto; } div.message div.right { width:250px; text-align:center; } div.message div.right hr { border-top:1px solid #ccc; margin-top:10px; } div.message div.right hr+span { background-color:#fff; font-weight:bold; padding:0 10px; position:relative; bottom:10px; } div.message div.right fieldset { margin:0 0 10px; padding:10px; text-align:left; position:relative; } div.message div.right fieldset.from { margin-right:50px; border-color:#6f6; background-color:#dfd; color:#040; } div.message div.right fieldset.to { margin-left:50px; border-color:#66f; background-color:#ddf; color:#004; } div.message div.right fieldset img.del { width:16px; top:0; right:8px; } div.message img.del { position:absolute; opacity:0.3; } div.message img.del:hover { cursor:pointer; opacity:1; } <!DOCTYPE html> <html lang="ru-ru" dir="ltr"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> <title>Test</title> </head> <body> <div class="message"> <div class="left"></div> <div class="right"> <hr/><span>08.10.2016</span> <fieldset class="from" id="mes-1475888362-1"> <legend>Ilya Indigo <span>03:59</span></legend> <img class="del" data-date="1475888362" data-fid="1" data-title="03:59" src="http://dolinasnov.com/uploads/delete.png" alt="del"/> <p>Тестовое сообщение<br/>новая строка<br/>ещё одна строка<br/>и ещё одна строка.</p> </fieldset> <fieldset class="from" id="mes-1475888608-1"> <legend>Ilya Indigo <span>04:03</span></legend> <img class="del" data-date="1475888608" data-fid="1" data-title="04:03" src="http://dolinasnov.com/uploads/delete.png" alt="del"/> <p>Ещё одно.</p> </fieldset> <fieldset class="to" id="mes-1475889423-2"> <legend>Ilya Indigo 2 <span>04:17</span></legend> <img class="del" data-date="1475889423" data-fid="2" data-title="04:17" src="http://dolinasnov.com/uploads/delete.png" alt="del"/> <p>Отвечаю.</p> </fieldset> <fieldset class="from" id="mes-1475892511-1"> <legend>Ilya Indigo <span>05:08</span></legend> <img class="del" data-date="1475892511" data-fid="1" data-title="05:08" src="http://dolinasnov.com/uploads/delete.png" alt="del"/> <p>Ещё одно.</p> </fieldset> </div> </div> </body> </html> Answer: The easiest and fastest way to declare all variables is via let instead of var .