JavaScript onmousedown event is not giving continuous event?

I want user mouse position while the user is holding down the mouse button continuously?

I try to do the with following code:

$(div).mousedown(function(eventObj){
    console.log(eventObj.clientX);  
});

but this code is giving mouse position only once when the user presses the mouse button.

Answer:1

The easiest way to accomplish this is to put the mousemove event into the mousedown event so that when the mousedown event triggers the mousemove event that fires continously also fires. This allows the mouse location to be tracked when the mouse button is held down. The mouseup event was added so that on release it stops tracking the mouse location from the mousemove event.

https://jsfiddle.net/q37z3g94/

$(

"#test").mousedown(function(eventObj) {
  $("#test").mousemove(function(event) {
    var pageCoords = "( " + event.pageX + ", " + event.pageY + " )";
    var clientCoords = "( " + event.clientX + ", " + event.clientY + " )";
    $("span:first").text("( event.pageX, event.pageY ) : " + pageCoords);
    $("span:last").text("( event.clientX, event.clientY ) : " + clientCoords);
  });
});

$("#test").mouseup(function(eventObj) {
  $("#test").unbind('mousemove');
});

Answer:2

For example, you could use the setInterval function to repeatedly call a function while the mouse button is down, and then use clearInterval to stop when the mouse button is released. Here is an example (using jQuery):

var interval;
$("#div").mousedown(function(eventObj) {
    interval = setInterval(performWhileMouseDown(eventObj), 100);
}).mouseup(function() {
    clearInterval(interval);  
});
function performWhileMouseDown(eventObj) {
    console.log(eventObj.clientX);
}
Answer:3

You could use mousedown and mouseup to set a flag - maybe isMouseDown, and then you can use clientX and clientY with that info where you need it

Answer:4

You're probably looking for the onMouseMove event. What you should do is onMouseDown start listening to the onMouseMove event with what you want done, then onMouseUp stop listening. Example code would look like:

$(div).mousedown(function(eventObj){
    $(div).mousemove(function(evt){
        console.log(''+evt.pageX+', '+evt.pageY);
    });  
});
$(div).mouseup(function(eventObj){
    $(div).off( "mousemove" );
});
Answer:5

I am using Selenium WebDriver and coding in Java. In a code I need to scroll down to a specific element in the web page for clicking it. I am using JavascriptExecutor command. My question is how will ...

I am using Selenium WebDriver and coding in Java. In a code I need to scroll down to a specific element in the web page for clicking it. I am using JavascriptExecutor command. My question is how will ...

I'd like to bind 2 Angular model variables to a range input element according to the following: update first model variable for every change of the range input update second model variable after x ms ...

I'd like to bind 2 Angular model variables to a range input element according to the following: update first model variable for every change of the range input update second model variable after x ms ...

  1. input bind variables

Given two points: const point1 = {x: 100, y: 0.95} const point2 = {x: 75, y: 1.05} I need to know the value of y when x equals some arbitrary value. Please assume a linear relationship exists. For ...

Given two points: const point1 = {x: 100, y: 0.95} const point2 = {x: 75, y: 1.05} I need to know the value of y when x equals some arbitrary value. Please assume a linear relationship exists. For ...

  1. function point line of code
  2. point function line
  3. function point and line of code difference

I am trying to get an input box to be disabled when a checkbox is checked. I'm trying to use Knockoutjs to get this done, but it doesn't seem to work. Here is my html: <input id="input1" type="...

I am trying to get an input box to be disabled when a checkbox is checked. I'm trying to use Knockoutjs to get this done, but it doesn't seem to work. Here is my html: <input id="input1" type="...

  1. knockout js disabled binding
  2. knockout js disable binding