Please help with the cycle. The fact is that all handlers loop hangs in value = poll

var granul = { photo_icon: document.getElementById('photo_icon1'), video_icon: document.getElementById('video_icon1'), quest_icon: document.getElementById('quest_icon1') } for (prop in granul) { var value if (granul[prop] == granul.photo_icon) { value = 'фото' } else if (granul[prop] == granul.video_icon) { value = 'видео' } else if (granul[prop] == granul.quest_icon) { value = 'опрос' } else { value = х } granul[prop].addEventListener('mouseover', function () { this.style.width = '40px' this.innerHTML = value }, false) } 
  • What does granul contain? - lampa
  • four
    Deja vu - Specter
  • @lampa granul contains the id of the elements - Zow
  • @Spectre, and I thought so) that this is garbage. Just the last time it was not much more - Zow

2 answers 2

Just the last time was not much different.

j instead of value ... it makes all the difference

and you did not think that you can simplify the cycle?

 var granul = { photo_icon: { el: document.getElementById('photo_icon1'), value: 'фото' }, video_icon: { el: ..., value: 'видео' }, quest_icon: { el: ..., value: 'опрос' } } 
  • @Spectre var granul = {photo_icon: {el: document.getElementById ('photo_icon1'), value: 'photo'}, video_icon: {el: document.getElementById ('video_icon1'), value: 'video' quest_icon: {el : document.getElementById ('quest_icon1'), value: 'poll'}} for (prop in granul) {granul [prop] .el.addEventListener ('mouseover', function () {this.innerHTML = granul [prop]. value}, false)} - Zow

If you simplify, then so

 var granul = [{ el: document.getElementById('photo_icon1'), val: 'фото' }, { el: document.getElementById('video_icon1'), val: 'видео' }, { el: document.getElementById('quest_icon1'), val: 'опрос' }]; granul.forEach(function(item){ item.el.addEventListener('mouseover', function(){ this.style.width = '40px'; this.innerHTML = item.val; }, false); }); 

Go

  • @vonica without jQuery? - Zow
  • one
    here without jquery - vonica
  • @vonica wait and forEach? - Zow
  • this is Array.prototype.forEach to jQuery irrelevant - Specter
  • I wrote about this, in jquery there is $ .each - vonica