Sem
Sem

Reputation: 4669

Mouse positions not given after trigger()

The problem is pretty straight forward:

$('#btn1').click(function(event){
    alert(
        "pageX: " + event.pageX +
        "\npageY: " + event.pageY
    );
});

$('#btn2').mouseout(function(e){
   $('#btn1').trigger('click');
});

The second button alerts undefined variables. Is there a way to trigger the click event while passing the mouse positions?

Here is a jsfiddle.

Upvotes: 3

Views: 217

Answers (1)

The Alpha
The Alpha

Reputation: 146249

Try this

$('#btn1').click(function(event){
    alert(
        "pageX: " + event.pageX + 
        "\npageY: " + event.pageY
    );
});

$('#btn2').click(function(e){
    $('#btn1').trigger(e);
});

Just pass the original event object that carries everything.

​DEMO.

Also you can create a custom event object and pass it as follows

$('#btn2').click(function(e){
    var evt={
        'type':'click',
        'pageX':e.pageX,
        'pageY':e.pageY
    };
    $('#btn1').trigger(evt);
});

DEMO.

Upvotes: 8

Related Questions