Ad

JQuery Trigger Document Event Into A DIV

I am totally new to jQuery. What I wanted to do is filter certain events from going into a DIV.

I have following in my page

<div id="overlay"></div> 
<div id="GameDiv" style="width:1280px; height: 720px;"></div>

overlay has following style

  <style>
  #overlay {
  position: fixed; /* Sit on top of the page content */
  display: block; /* Hidden by default */
  width: 100%; /* Full width (cover the whole page) */
  height: 100%; /* Full height (cover the whole page) */
  top: 0; 
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(1,0,0,0.5); /* Black background with opacity */
  z-index: 200; /* Specify a stack order in case you're using a different order for other elements */
  pointer-events: auto
}
  </style>

I have following code

$(document).on(
{
    mousedown:mouseEvent,
    mouseup:mouseEvent,
}); 
$("GameDiv").on(
{
    mousedown:divEvent,
    mouseup:divEvent,
});
function divEvent(e)
{
    console.log("div event"+e);
}
function mouseEvent(e)
{ 
    console.log("doc event" + e);
    var evnt =  jQuery.Event(e.type,e);
    $("GameDiv").trigger(evnt)
}

Live Example:

$(document).on(
{
    mousedown:mouseEvent,
    mouseup:mouseEvent,
}); 
$("GameDiv").on(
{
    mousedown:divEvent,
    mouseup:divEvent,
});
function divEvent(e)
{
    console.log("div event"+e);
}
function mouseEvent(e)
{ 
    console.log("doc event" + e);
    var evnt =  jQuery.Event(e.type,e);
    $("GameDiv").trigger(evnt)
}
#overlay {
  position: fixed; /* Sit on top of the page content */
  display: block; /* Hidden by default */
  width: 100%; /* Full width (cover the whole page) */
  height: 100%; /* Full height (cover the whole page) */
  top: 0; 
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(1,0,0,0.5); /* Black background with opacity */
  z-index: 200; /* Specify a stack order in case you're using a different order for other elements */
  pointer-events: auto
}
<div id="overlay"></div> 
<div id="GameDiv" style="width:1280px; height: 720px;"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

I am getting the document mouse event. But I am not receiving 2nd div event.

What did I do wrong?

Is there a better way of doing this?

Ad

Answer

To get the both the events you can follow the below code snippet.

$(document).on(
{
    mousedown: mouseEvent,
    mouseup: mouseEvent,
});
    $("#GameDiv").on("click", divEvent);
    
    function divEvent(e) {
        console.log("div event" + e);
    }
    function mouseEvent(e) {
        console.log("doc event" + e);
        var evnt = jQuery.Event(e.type, e);
        $("GameDiv").trigger(evnt)
    }
#overlay {
  
  display: block; /* Hidden by default */
  width: 100%; /* Full width (cover the whole page) */
  height: 100%; /* Full height (cover the whole page) */
  top: 0; 
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(1,0,0,0.5); /* Black background with opacity */
  z-index: 200; /* Specify a stack order in case you're using a different order for other elements */
  pointer-events: auto
}
<div id="overlay"></div> 
<div id="GameDiv" style="width:1280px; height: 720px;"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

If you really need to do it with mousedown and mouseup events u can follow the below snippet.

 #overlay {
  
  display: block; /* Hidden by default */
  width: 100%; /* Full width (cover the whole page) */
  height: 100%; /* Full height (cover the whole page) */
  top: 0; 
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(1,0,0,0.5); /* Black background with opacity */
  z-index: 200; /* Specify a stack order in case you're using a different order for other elements */
  pointer-events: auto
}
<div id="overlay"></div> 
<div id="GameDiv" style="width:1280px; height: 720px;"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
    $(document).on(
{
    mousedown: mouseEvent,
    mouseup: mouseEvent,
});
    $("#GameDiv").on("mousedown", divEvent);
    $("#GameDiv").on("mouseup", divEvent);
    function divEvent(e) {
        console.log("div event" + e);
    }
    function mouseEvent(e) {
        console.log("doc event" + e);
        var evnt = jQuery.Event(e.type, e);
        $("GameDiv").trigger(evnt)
    }
</script>

Another solution

Refer the snippet below to call both the events at the same time

 #overlay {
  
  display: block; /* Hidden by default */
  width: 100%; /* Full width (cover the whole page) */
  height: 100%; /* Full height (cover the whole page) */
  top: 0; 
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(1,0,0,0.5); /* Black background with opacity */
  z-index: 200; /* Specify a stack order in case you're using a different order for other elements */
  pointer-events: auto
 <div id="overlay"></div> 
<div id="GameDiv" style="width:1280px; height: 720px;"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
    $(document).on(
{
    mousedown: mouseEvent,
    mouseup: mouseEvent,
});
   
    $("#GameDiv").on({
        "mousedown": divEvent,
        "mouseup": divEvent
    });
    function divEvent(e) {
        console.log("div event" + e);
    }
    function mouseEvent(e) {
        console.log("doc event" + e);
        var evnt = jQuery.Event(e.type, e);
        $("GameDiv").trigger(evnt)
    }
</script>

Ad
source: stackoverflow.com
Ad