views:

370

answers:

4

Hello all,

Typically, when needing to access an event, you do so via the parameter specified in the callback function:

$button.live("click", function(ev) {
  // do something with ev here, like check 'ev.target'
}

But instead (for reasons too complicated to get into here), I do not want to use an anonymous callback function, but instead specify a function to call, like this:

$button.live("click", functionToCall(ev, $(this));

So you'll notice that I included 'ev' as a parameter to functionToCall(), but this obviously won't work because I'm not using the anonymous callback function. But I do still need to access that click event (to check ev.target) within functionToCall(). My question is, how do I access this event? It would be nice if I could do something like this:

$button.live("click", functionToCall($(this));

and

function functionToCall($item) {

   var target = $item.event("click").target;
   // do something with target
}

Any ideas would be very much appreciated. Thanks.

+2  A: 

You could call a function within the anonymous callback function:

$button.live("click", function(ev) {
    functionToCall(ev, $(this));
}

EDIT: I think this may be what you're looking to do (untested):

function handleClick(ev) {
    $(this).die("click");
    // ...whatever processing to do...
    $(this).live("click", handleClick);
}

$button.live("click", handleClick);

I believe the $(this) will refer to the button object in which the function was called.

CAbbott
I did think of that, but the reason I can't do that gets into what I said was too complicated to get into. But in short, I'm trying to unbind the live click event while the click is occuring (using die()), and need to do that at the beginning of the click code. At the end, I need to rebind to a live click event, but will need to recall that same function again, but with the same code in that one as well, and repeat on forever. So this option won't work, but thanks for the idea.
Mega Matt
+9  A: 

Original answer

function test(eve) {
  alert(eve.type);
  alert(this);
  //$(this) if you need it as jQuery object
}
$([yourselector]).live("click", test);

You will automatically get the event in the eve parameter.


Answer to extended question in comment

Passing in a parameter makes it a little more difficult. If you need an explanation why I did it like this: Ask.

function helper(customparam) {
    return function(eve, selector) { actualFunction(eve, selector, customparam, this) };
}

function actualFunction(eve, selector, customparam, self) {
    alert(eve.type);
    alert(selector);
    alert(customparam);
    alert(self); //self is now the element we clicked on
    //$(self) if you need it as jQuery object
    //using this won't work anymore as this is now window
}

$([yourselector]).live("click", helper([yourparameter]));
jitter
@jitter, thanks for the quick response. Follow-up question: If I need to pass a parameter to function ('test()' in your case), how would it work there? If I called it as $([yourselector]).live("click", test($(this)); and then declared the function as function test(eve, $item) {}, would that work? Thanks.
Mega Matt
a little over my head, but luckily the only parameter I need to access from the function is $(this). But this looks like an excellent option for those that need to pass in something other than $(this), and still need to access the event. Thanks for the edited response.
Mega Matt
Hmm so you mean the first answer actually already does what you want
jitter
The technique in the expanded answer is basically a poor-man's function-currying, which is novel but I would argue it's quite unconventional in JS. That could pose problems for other developers who may have to maintain this code in the future. If you find yourself needing to pass stuff into an event handler, you should re-evaluate the design of your code to move the data your handler needs into the DOM element by using jQuery data or expandos.
gWiz
Yes, it did do what I want, but your extended answer is useful information that is related to another post I made because I think others will want to know (btw, this was before your extended answer): http://stackoverflow.com/questions/1798828/accessing-parameters-and-events-in-function-from-jquery-event
Mega Matt
@gWiz currying is know to me. This is just a special tailored version for what I thought where his needs. Regarding the other statements I can't really follow your point-of-view. Of course it needs documentation but that also holds true for every other implementation. And using `data()` might even be less obvious depending on who sets the data and retrieves it.
jitter
A: 
var mythis = $(this);
var callback = function(ev) {
    var target = mythis.event("click").target;
}


$button.live("click", callback);
Josh Pearce
+1  A: 

Remember that jQuery re-assigns this when it calls event handlers, by using the Function methods call or apply. So when functionToCall is invoked, this is the DOM element of $button.

var functionToCall(ev) {
    var $this = $(this);
    $this.die("click", functionToCall);
    // stuff
    $this.live("click", functionToCall);
}

$button.live("click", functionToCall);
gWiz