You want to call the function in the proper context. There is Function#call for that purpose. You also need to use a normal function, as "fat arrow" functions handle this differently.
var addClass = function (className) {
if (this.classList.contains(className)) {
this.classList.remove('active');
this.classList.add(className);
} else {
this.classList.add(className);
}
}
exploreStory_buttons.forEach(el => {
el.addEventListener('mouseover'); // where is the event listener??
addClass.call(this, active); // N.B. active is undefined here. Do you mean "active"?
});
You also do not want to abuse language features to write "smart" code. Use if/else and multiple lines for multiple statements, you are not a code obfuscator.
Assuming you want to add a "hover" behavior, the code would have look something like this:
function addClass(className) {
return function () {
this.classList.add(className);
}
}
function removeClass(className) {
return function () {
this.classList.remove(className);
}
}
var activate = addClass('active');
var deactivate = removeClass('active');
exploreStory_buttons.forEach(el => {
el.addEventListener('mouseenter', activate);
el.addEventListener('mouseleave', deactivate);
});
Note how addClass and removeClass return functions that can serve as event handlers. Storing them into activate and deactivate variables means you are not creating multiple equivalent copies of a function inside the loop, but in fact assign the same function to all elements.
The activate/deactivate variables and function-returning functions can be avoided by implementing a hover() function (jQuery has a similar feature):
function hover(elements, onenter, onleave) {
elements.forEach(el => {
el.addEventListener('mouseenter', onenter);
el.addEventListener('mouseleave', onleave);
});
}
hover(exploreStory_buttons, function () {
this.addClass('active');
}, function () {
this.removeClass('active');
});
Here the event handler functions are created only once and directly where they are needed. Much better than storing them in helper variables.
addClass(active):activeis an undefined variable, therefore the parameter you pass to the function is undefined. Should be"active".&is a bit-wise operator probably you have meant&&for a short circuit operation. Besides why do you add the same class if it is already there..?