0

I have a working script to apply transformations to SVG so that the image is draggable. The script works when I use the attributes of the html element.

<g id="all" onmousedown="startMove(evt)" onmousemove="moveIt(evt)" onmouseup="endMove(evt)">

However I would like to bind the events with Jquery something like below and wonder where I am going wrong with the code below

$('svg > g').mousedown(startMove(evt));
$('svg > g').mousemove(moveIt(evt));
$('svg > g').mouseup(endMove(evt));
1
  • The event expects a function but you're passing the return value of the function. Functions are objects too, you should pass a reference not execute it. So el.event(fn) not el.event(fn()) Commented Sep 4, 2013 at 8:22

2 Answers 2

1

Try this:

$('svg > g').mousedown(function(evt){
    //do stuff
});

or even

$('svg > g').mousedown(startMove);

function startMove(evt){
    //do stuff
}

startMove references the actual function, while startMove() uses it's return value.

Sign up to request clarification or add additional context in comments.

2 Comments

I think the second one only works when you have no arguments to pass to the function
@PeterSaxton in this case it should work because it is expecting the event as the argument
0

I believe you have to do it like so:

$('svg > g').mousedown(function(evt){
    startMove(evt);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.