5

I'm trying to manually trigger a mousemove event with jQuery. Demo in this fiddle http://jsfiddle.net/qJJQW/

From other similar posts on Stack Overflow it seems that this should work. Why isn't it?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
hofnarwillie
  • 3,563
  • 10
  • 49
  • 73

2 Answers2

4

Use jQuery to bind the mousemove event:

$(function () {
   $("#test").on("mousemove", youCantHandleTheFunc);

    $('#button').click(function () {
        $('#test').trigger('mousemove', {type:'custom mouse move'});
    });
});

function youCantHandleTheFunc (e,customE) {
    if (customE != undefined) {
         e = customE;   
    }
    $('#result').html(e.type);
}

Your updated fiddle.

karim79
  • 339,989
  • 67
  • 413
  • 406
  • Thank you, much appreciated. I thought that might be it but then stumbled on this line in the jQuery docs `"To trigger handlers bound via jQuery without also triggering the native event, use .triggerHandler() instead."` which says to me that the alternative `trigger()` function does trigger the native events (maybe my understanding of native events is limited. :) – hofnarwillie Oct 22 '13 at 10:53
4

jQuery's trigger() only triggers event handlers set with jQuery ?

$(function(){
    $('#test').on('mousemove', youCantHandleTheFunc); 

    $('#button').click(function(){
        $('#test').trigger('mousemove',{type:'custom mouse move'});
    });
});

function youCantHandleTheFunc(e,customE){
    if (customE!=undefined){
         e=customE;   
    }
    $('#result').html(e.type);
}

FIDDLE

adeneo
  • 312,895
  • 29
  • 395
  • 388