4

I have two div :

<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 trigger click eventhandler of div1 to execute on clcicking of div2. For this i wrote:

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

It's working fine but the problem is i am not able to get e.pageX and e.pageY in the event handler of div1. How to pass eventdata of div2 click event handler i.e e to div1 click event handler. Please help.

5
  • Woah, what happened with the title? I can only assume that was a mistake. Commented Feb 2, 2011 at 11:31
  • I don't think it is possible due to the trigger function only running the function you specified and not actually firing the event. This means that pageX/Y will not exist. e won't even exist. Commented Feb 2, 2011 at 11:33
  • @Wolfy, e does exist as jQuery creates one as part of .trigger() but pageX and pageY will be undefined. Commented Feb 2, 2011 at 11:38
  • @Niraj, no worries, it just made me read the question 5 times ;) Commented Feb 2, 2011 at 11:39
  • Oh I see. Thanks for clearing that up. I don't use jQuery too much anymore :] Commented Feb 2, 2011 at 11:39

2 Answers 2

6

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

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

For events of a different type, you may 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);
});
Sign up to request clarification or add additional context in comments.

Comments

0

jQuerys .trigger()help should be the answer here. You can pass in event-strings (along with parameters) aswell as event objects.

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.