Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Howtoaddafunctiontoanelementwithanotherfunction

I want to create a function, similar to this:

var elementsArray = document.querySelectorAll('whatever');

elementsArray.forEach(function(elem) {
    elem.addEventListener("input", function() {
        //this function does stuff
    });
});

How to addeventlistener to multiple elements in a single line

The difference is, that I want to pass the element and the function to another function that will join them together.

function joinElementAndFunction(element,customFunction){
  let listOfElements = document.querySelectorAll(element);

  listOfElements.forEach(function(e) {
      e.addEventListener('click', customFunction);
  });
}

function doSomething(){
  // does magic
}

joinElementAndFunction('input',doSomething);
joinElementAndFunction('a',loadPage);

Is this even possible to do ?

Comments