i want to apply event listeners in javascript to elements inside a loop. The event name is variable in the loop. When i try with my code it's not working only the last addEventListener is working ? The events i want to add are : click and mouseenter. When i click on the item, it's the mouseenter event which which is fired !
_apply = function(trackEventData, trackEventItem) {
var tmpData, i, j, eventType, eventAction;
// get stringified json events
tmpData = JSON.parse(trackEventData);
for (i = 0; i < tmpData.length; i++) {
// for each type in events
eventType = Events.getEventTypeByKey(tmpData[i].key);
eventAction = tmpData[i].actions;
console.log('apply ' + eventType.event);
// we add the event listener
trackEventItem.addEventListener(eventType.event, function(e) {
console.log('eventfired ' + eventType.event);
e.preventDefault();
// and for each action we add the function callback
for (j = 0; j < eventAction.length; j++) {
Events.getEventActionByKey(eventAction[j].value).action(eventAction[j].options);
}
});
}
};