I am trying to define some hover properties using JavaScript. So, I decided to go with "mouseenter" and "mouseleave" events for an element. Now, I have to call the same function for both the events in addEventListener.
So, is there any way of doing it like:
element.addEventListener("mouseenter" or "mouseleave", myfunc);
3
Answers
Well you cannot use an "or" to add multiple listeners but you can create one function and add multiple listeners to it. Here is an example:
Well if you need both events, then you should add two event listeners, so the syntax should look like this.
or
It’s the same thing
For this exact syntax you should override the
addEventListener
:An one-liner without overriding: