How to trigger a mouse move event from jQuery

hofnarwillie picture hofnarwillie · Oct 22, 2013 · Viewed 15.1k times · Source

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?

Answer

karim79 picture karim79 · Oct 22, 2013

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.