How can I create an event in JQuery triggered when the mouse is down and moving? And only triggered once each mousedown + mousemove?
-
I'm encountering this very problem today. Apparently, with Firefox, we're just screwed? The document.mouseup doesn't work either. – Ducain Jan 06 '11 at 18:00
13 Answers
Updated:
So, it looks like if your mouse is no longer over the element on which onmouseup is bound, it won't see the mouse up event. Makes sense, when you stop and think about it, but when the mousedown event happens over the element, we expect, as UI users, for it to know when it was released (even if it isn't over the element).
So, to get around this, we actually detect the mouseup on the document level.
var clicking = false;
$('.selector').mousedown(function(){
clicking = true;
$('.clickstatus').text('mousedown');
});
$(document).mouseup(function(){
clicking = false;
$('.clickstatus').text('mouseup');
$('.movestatus').text('click released, no more move event');
})
$('.selector').mousemove(function(){
if(clicking == false) return;
// Mouse click + moving logic here
$('.movestatus').text('mouse moving');
});
I tried this out on jsbin, and it seems to work. Check it out here: http://jsbin.com/icuso. To edit it (see the JS and HTML), just tag "edit" on the end of the URL. http://jsbin.com/icuso/edit.

- 41,216
- 30
- 109
- 147
-
the problem is that the mouseup doesn't seem to be triggered after I have dragged the mouse, then the clicking isn't reset – Oct 15 '09 at 14:41
-
How interesting - I didn't realize that. I just tested it on FF3.5 and it is as you say. Which browser are you using? – Matt Oct 15 '09 at 15:00
-
Yeah, its firefox. And it gets worse. I'm working with drag and drop from an iframe and out to the parent. So I guess I'll need a mouseup trigger in the parent aswell, which will trigger mouseup in the iframe. I'll try this – Oct 15 '09 at 15:12
-
-
@Persmon 3 years too late but if you want mouseup to work on iframes (and the belong to the same domain) all you have to do is replace $(document) with $('iframe').map((i, e) => e.contentWindow.document).add(document) – Ivan Castellanos Nov 09 '18 at 05:45
use following code
$(element).mousemove(function(e){
if(e.which==1)
{
#do job
}
});
Update :
For some browsers you may want to use this :
$(element).mousemove(function(e){
if(e.buttons==1)
{
#do job
}
});

- 4,035
- 1
- 35
- 39
-
1
-
1thanks a lot! shortest solution what i have ever seen for this kind of problem! – LINKeRxUA Mar 29 '16 at 16:35
I did the following for an audio volume controller:
$('#control-vol').bind('mousedown', function(e){
$('#control-vol').bind('mousemove', function(e){
var volumen = e.pageX - this.offsetLeft;
$('#barra-vol').width(volumen + 'px');
$('audio')[7].volume = volumen/50;
});
$('#control-vol').bind('mouseup',function(){
$('#control-vol').unbind('mousemove')
});
});

- 4,923
- 4
- 30
- 38

- 81
- 1
- 2
document.onmousedown = function(e) {
e = e || window.event;
var button = (type e.which != "undefined") ? e.which : e.button;
if (button == 1) {
alert("Left mouse button down");
}
};
$("element").mousedown(function () {
$(this).mousemove(function () {
console.log("OK Moved!");
});
}).mouseup(function () {
$(this).unbind('mousemove');
}).mouseout(function () {
$(this).unbind('mousemove');
});

- 11
- 2
This might help the situation... there is some difficulty when dealing w/ iframes. I'm currently working on a project needing to resize elements in an iframe.
Say you have an iframe with an id of 'iframe'. I have only tested this in FF, but it's working:
var $iframe = $('#iframe');
$iframe.load(function()
{
var $body = $iframe.contents().find('HTML');
/* ...bind your events here to $body.. */
$body.mousedown(function(){...});
$mody.mouseup(function(){...});
}
I try to bind everything to the document, or in this case, to the iframe's document, and then when an event happens, I call a function that determines what exactly was the target. Keep in mind that jQuery has a pretty cool feature in that you can wrap the event target in a jQuery function:
$(selector).click(function(e)
{
var id = $(e.target).attr('id');
});
Hope this helps! And I hope it works in the other browsers or I'm going to be really frustrated in a few days :o

- 21
- 2
The answer can be found in another SO topic:
Firefox drags div like it was an image
You need to return false from the event handlers to prevent the default action
Once I put a return false in my event handlers, it worked like a charm.
For anyone else coming to this thread in search of generic assistance for jQuery mouseup
, mousemove
and mousedown
using dynamically loaded content, I found (In chrome at least) this modification of Matt's post to be functional:
var clicking = false;
$('.selector').live("mousedown",function(){
clicking = true;
$('.clickstatus').text('mousedown');
});
$(document).live("mouseup",function(){
clicking = false;
$('.clickstatus').text('mouseup');
$('.movestatus').text('click released, no more move event');
})
$('.selector').mousemove(function(){
if(clicking == false) return;
// Mouse click + moving logic here
$('.movestatus').text('mouse moving');
});
http://api.jquery.com/live/
jQuery $.live()
binds to all present and future elements, so if you need to add a mousemove
and mousedown
to a dynamically created element, this should work (again, in chrome at least).
setTimeout(function(){
var radiobox_state;
$('input[type="radio"]').mousedown(function(){
radiobox_state=this.checked
})
$('input[type="radio"]').click(function(){
if(radiobox_state){
$(this).prop("checked",false);
}
})
}, 500);
-
1Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 13 '21 at 10:38
$(document).on('mousedown', function() {
$(document).bind('mousemove', function() {
$('#id-name').dosomething();
});
});
$(document).on('mouseup', function() {
$(document).unbind('mousemove');
});

- 635
- 5
- 6
-
-
1why use `on` and `bind` together in the same code? they are equivalent - you should pick one or the other. – UpTheCreek Feb 12 '12 at 14:45
-
UpTheCreek, i used bind so that i could unbind when the mouse is up. You are probably right, i could have used .on and .off instead of .bind and .unbind – djrconcepts Jul 02 '13 at 15:19
we use $.bind() like this:
$('#div').bind('mousemove mousedown', function(){});
This mean either mousemove or mousedown fired, the function would execute. so in the function, we still able to check if left button is press by using event.which equal to 1. so the code should look like this:
$('#div').bind('mousemove mousedown', function(e){
if(e.which == 1){
//let the fun begin
}
});

- 37
- 1
- 6
May be:
$('input').mousedown(function{
$('input').mousemove({
etc
});
});
but for some reason i think you already tried that..

- 915
- 2
- 8
- 29
-
ok, so $('input').bind('mousedown mousemove', fn); will only fire when the mouse is pressed down AND moving? I thought this would fire if the mouse is pressed down OR moving. – Oct 15 '09 at 14:07