I currently have a substantial amount of code containing multiple event listeners on various HTML objects, all using the 'click' listener. However, I now realize that in addition to these click listeners, I also want mouseenter listeners on all of these objects. As I started writing out the code manually, it occurred to me that there must be a simpler way to achieve this. Although it may seem unclear, here is an overview of what I currently have:
object1.addEventListener("click", function(){
if(condition1){
function1();
}
});
object2.addEventListener("click", function(){
if(condition2){
function2();
}
});
and so forth
My goal is to retain these functions and add new ones with 'mouseenter' listeners. These new functions will perform the same action as the click listeners if the specific condition for each object is met. Here is the desired outcome:
object1.addEventListener("click", function(){
if(condition1){
function1();
}
});
object1.addEventListener("mouseenter", function(){
if(condition1){
myAnimation();
}
});
object2.addEventListener("click", function(){
if(condition2){
function2();
}
});
object2.addEventListener("mouseenter", function(){
if(condition2){
myAnimation();
}
});
and so forth
As mentioned, although I could manually write out all of this code, there are over 100 functions involved. It would be fantastic if there was a way for JavaScript to generate this automatically using a function.
EDIT:
My current project involves constructing a storyline (in VR using Aframe). There are numerous elements within the scene that can be clicked only if they align with the story's progression, hence the conditions in the listeners. What I am trying to accomplish now (which dawned on me belatedly) is to introduce an animation with the cursor whenever it hovers over the correct clickable element, providing users with a visual cue. Therefore, all of the 'mouseenter' listeners will share the same specific condition as their corresponding 'click' listeners. I understand this explanation might still sound vague, but unfortunately, I'm unsure how to elaborate further.