0

I have a div:

<div id="myDiv">This Div is Tracked</div>

I need a function to do the following:

Example:

checkSeveralEvents = () => {

    if (myDivIsHovered || myDivIsClicked || myDivIsFocus) {

        console.log(`myDiv has been ${eventHere}`);

    }

}

How can I do this?

  • You should take a look at this : http://stackoverflow.com/questions/11845678/adding-multiple-event-listeners-to-one-element – Dr.Blamo May 14 '17 at 10:48
  • On div changes depend on it you can fire event with passing additional data, like `.fire('myDivIsChanged', {type: "focus"})`, `.fire('myDivIsChanged', {type: "clicked"})` and catch it in one function, and then handle it, depends on type being passed – DanilGholtsman May 14 '17 at 10:51
  • Who or what calls `checkSeveralEvents`? You might want to get familiar with how to react to a single event type first. – Bergi May 14 '17 at 10:58

1 Answers1

1

You can just listen to all needed events one by one:

const d = document.getElementById('myDiv');
const events = ['click', 'mouseleave', 'mouseover'];

events.forEach(e => {
  d.addEventListener(e, (ev) => {
    console.log('My div has been ', e + 'ed')
  })
})
<div id="myDiv">This Div is Tracked</div>
Egor Stambakio
  • 17,836
  • 5
  • 33
  • 35