Question

I'm having problems with mousemove in jquery. I want to check the coordinates of the mouse pointer AFTER a mousedown event AND a mousemove event, but it updates only once and the result is only the coordinates at the moment of the mousedown event.

I really need some advices, thanks :)

I post only a short extract of my original code here:

P.S. I tried to use "$(document).ready(function(){", but without success.

<!DOCTYPE html>
<head>
<title>jQuery Test</title>
   <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
   <script type="text/javascript" language="javascript">
    function my_func(event){
        $("#log1").html("mouse is down");
        var x = $(this);
        //some code
        $(document).ready(function(){
            x.on("mousemove", function(){
                $("#log1").html("x:"+event.pageX+" y:"+event.pageY);
            });
        });
    }

    $(document).ready(function(){
        $("button").on("mousedown",my_func); //it needs to be a named function because I will call it multiple times
    });
   </script>
</head>
<body>
   <button >TEST</button> 
   <div id="log1"></div>
</body>
</html>
Was it helpful?

Solution

Change

x.on("mousemove", function(){

to

x.on("mousemove", function(event){

JsFiddle -> http://jsfiddle.net/6y83H/

OTHER TIPS

Did you forget to pass event on your mouse movement function?

Try this fiddle fiddle

function my_func(event) {
    $("#first").html("mouse is moving</br>x:" + event.pageX + " y:" + event.pageY);
}

function my_func1(event) {
    $("#first").html("mouse is now down</br>x:" + event.pageX + " y:" + event.pageY);
}

$(document).ready(function () {
    $("#first").on("mousemove", my_func);
    $("#first").on("mousedown", my_func1);
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top