Search code examples
javascriptaddeventlistenermouseoveronmouseoverondblclick

How to add 2 addeventlisteners to work together (e.g.: run event on dblclick when mouse pointer is at left side of screen)


New to JavaScript, trying to figure out how to combine addEventListener events.

For example, I want to trigger something when I double-click the side of the screen, within 30 or 40 pixels.

I assume I need to combine these somehow:

  • document.addEventListener("dblclick",function (event) {
  • document.addEventListener("mouseover", (event) => {

How is that done? I'm also struggling to get that mouseover working even alone, if anyone cares to take a stab at it!


Solution

  • You don't need mouseover event listener to do that.

    Method 1: First create 30-40pixel div element, with position:fixed. Then add double click event to that div element.

    Method 2: Add doubleclick event to the document, read event.clientX (or event.screenX). if (event.clientX < 30), run what you want.

    document.addEventListener("dblclick",function (event) { 
            if(event.clientX < 30 ) {
            // some logic
            }
    })