So my dilemma is that I don\'t want to write the same code twice. Once for the click event and another for the touchstart event.
Here is the original co
This is my solution in which I deal with multiple events in my workflow.
let h2 = document.querySelector("h2");
function addMultipleEvents(eventsArray, targetElem, handler) {
eventsArray.map(function(event) {
targetElem.addEventListener(event, handler, false);
}
);
}
let counter = 0;
function countP() {
counter++;
h2.innerHTML = counter;
}
// magic starts over here...
addMultipleEvents(['click', 'mouseleave', 'mouseenter'], h2, countP);
MULTI EVENTS DEMO - If you click, move away or enter the mouse on the number, it counts...
0