views:

78

answers:

3

Hi everybody,

I have an interesting situation, I need to trigger a live click , because simple click doesn't work .

this is what I have:

$('.text').trigger('click');

but i need something like this :

$('.text').trigger(live('click' ...));

or something to fix this problem

this is my code :

$(".Sets a.largeImage").fancybox({

  'onComplete'          : function(){
        return errorimage(myurl);
  }

});

function errorimage(url) {

     $("#fancybox-img").live('click', function(){
         $('.lightbox:first').trigger('click');
     });

     $('#fancybox-img').trigger('click');

}   

The idea is that I wanna trigger $('.lightbox:first').trigger('click');, but in live mode , because simple click doesn't work !

Thank you !!!!

+1  A: 

The best solution would be to put your click handler in a separate function.

You can then call this function both from the live click handler and when you want to manually trigger the click.

SLaks
I cannot trigger manually, because this fuction i have to call , when another function is "onComplete "
Alexander Corotchi
What do you mean? You can call the function both in the `live` handler and elsewhere (including `onComplete`)
SLaks
A: 

I belive you can just use your first example as trigger is only used to activate a bound element, not to bind it..

So when your do:

$('#someElement').live('click',function(){./*..*/});

this is the same as

$('#someElement').click(function(){./*..*/}); 

apart from the first one also checks for new html on every ajax requests and binds again.

so just do:

$('div.class').trigger('click');

and it should trigger the live elements

RobertPitt
Wrong. `live` is very different from `bind`. You're confusing it with `livequery`, which does work like that.
SLaks
yes i understand that live is different but once the ajax content as come back and injected into the DOM, live takes into place and binds the elements before the task is copmpleted, once inside the dom and bound to the click, trigger should also work for the "new content"
RobertPitt
You're wrong. Live does not bind any elements. Calling `live` will handle the event on the root element, which will see every event that bubbles up. It then checks `e.target` and raises any handlers that subscribed to that target.
SLaks
@RobertPitt - @SLaks is correct, `.live()` doesn't do **any** work when AJAX events happen, the DOM gets modified, etc. It's a passive listener, waiting for events to bubble.
Nick Craver
Ok then my apologise for that, ill have to read up about the way live works in more detail. thanks for that.
RobertPitt
+1  A: 

I guess the problem you're facing is, that live() and delegate() don't bind an event handler to the DOM element / jQuery object itself, but at some parent node. live() for instance, will bind an handler to the document.body which checks the event.target. In simple words, it makes usage of event bubbling, thats the idea of live events.

In other words, using live, it might be possible to trigger an event like

$(document.body).trigger({ 
   type:   'click',
   target: $('.text')[0]
});

update

unfortunatly, that does not seem to work. I tried to also set the currentTarget and relatedTarget. Where does .live() bind an handler to? somebody?

jAndy