View Full Version : Drag - not catching my mouse events

12-08-2011, 06:46 PM
Just registered, because I'm trying to figure out some dragging stuff and it's not working the way I'd like and I really can't figure out why.

I made a drag element (image). I just want to apply the code to this element.

This is what happens:
1. I click on the image
2. I hold the mouse down and drag
=> the element don't follow
3. I let go of the mouse button
=> the element gets dragged
4. I click again
=> the element stops dragging

This is what I would like to happen (obviously):
1. I click the image
2. Hold the mouse down and drag
=> element follows
3. Mouse up
=> element stops following

var dragSko;
var dragSkoUrsp;

document.onmousedown = startaKod;
document.onmouseup = musUpp;

function startaKod(e) {

if (e.target.id == 'dragsko') {

startX = e.clientX;
dragSkoUrsp = parseInt(e.target.style.left);
document.onmousemove = musFlyttDrag;
dragSko = e.target


function musFlyttDrag(e) {
dragsko.style.left = (dragSkoUrsp + e.clientX - startX) + 'px' ;


function musUpp(e) {
document.onmousemove = null;

I'd very much appriciate if some kind soul had a look and explained what I've done wrong.

12-08-2011, 08:49 PM
Was a problem on another place in the code.