and I have the following jquery fo...">

Pass eventdata to event handler in javascript

I have two divs:

<div id="div1"></div> <div id="div2"></div> 

and I have the following jquery for div1:

  $('#div1').click(function(e) { alert(e.pageX + ' ' + e.pageY); }); 

. Now, I want to run the click handler for div1 to execute clcicking div2. For this, I wrote:

 $('div2').click(function(e) { $('#div1').trigger('click'); }); 

It works fine, but the problem is that I cannot get e.pageX and e.pageY in the div1 event handler. How to pass eventdata of div2 event handler for ie e to click1 event handler div1. Please, help.

+4
source share
2 answers

Since the event you want to trigger is of the same type, you can pass the old event object directly:

 $('#div2').click(function (e) { $('#div1').trigger(e); }); 

For events of a different type, you can create a custom event object:

 $('#div2').mouseenter(function (e) { var newE = jQuery.Event('click'); newE.pageX = e.pageX; newE.pageY = e.pageY; $('#div1').trigger(newE); }); 
+6
source

jQuerys .trigger() help should be here. You can pass event strings (along with parameters), as well as event objects .

 $('#div2').click(function(e) { $('#div1').trigger(e); }); 
0
source

Source: https://habr.com/ru/post/1338086/


All Articles