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?

3
  • You should take a look at this : stackoverflow.com/questions/11845678/… Commented May 14, 2017 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 Commented May 14, 2017 at 10:51
  • Who or what calls checkSeveralEvents? You might want to get familiar with how to react to a single event type first. Commented May 14, 2017 at 10:58

1 Answer 1

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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.