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?

0

2 Answers 2

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.

1
  • 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. :) Oct 22, 2013 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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.