views:

284

answers:

2

I'd like to attach a handler to an element using either jQuery live() or delegate(). Looking at the docs I see I can attach the handler for a custom event.

Is this possible for either of these jQuery functions to also trigger the handler? Basically I want to attach the handler function and run call it once.

Thank you for any help.

+2  A: 

If you only want to call it one time, then you might be better off with the .one() method.

If you want to invoke an event that has already been bound you can call the .trigger() method.

$('#foo').bind('click', function() {
      alert($(this).text());
    });
    $('#foo').trigger('click');
Paul Alexander
Hey Paul thanks for you help. I'm hoping to take advantage of the way either live() or delegate() find elements matching a selector in the future after the page is loaded. When an element is added to the DOM matching the selector live() or delegate() will add the handler but I'd also like to run the handler function right after it's attached.
patrick
A: 

Try delegating the ready event as well-- with the same event handler of course.

Jeremy