Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event in a function with jQuery

I know how to use events with jQuery in the classical way for example:

$("#moveArea").mousemove(function(event){
    $("#info").empty().append("pageX is: "+event.pageX);
});

Demo: http://jsfiddle.net/Ey7kP/

My question is how to pass the event in a function I have already create. I need something like the following, but I don't know how to achieve this

function cursorPos(event) {
  $("#info").empty().append("pageX is: "+event.pageX);      
}

$("#moveArea").mousemove(cursorPos(event));
like image 514
Sotiris Avatar asked Mar 03 '26 09:03

Sotiris


1 Answers

Just do

$("#moveArea").mousemove(cursorPos);

Since you're referring to the function and not calling it, there's no need for passing the arguments. jQuery will call it for you and pass event to it.

like image 119
Alex Turpin Avatar answered Mar 04 '26 22:03

Alex Turpin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!