Search code examples
jquerymousemovemousedown

mousemove event updating only once after mousedown


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>

Solution

  • Change

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

    to

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

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