how to trigger Live() click in jquery
Asked Answered
D

6

13

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 want to trigger $('.lightbox:first').trigger('click');, but in live mode, because simple click doesn't work!

Thank you !!!!

Dugger answered 30/6, 2010 at 13:48 Comment(1)
Can you share the context of this problem?Togliatti
M
10

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.

Moyna answered 30/6, 2010 at 13:51 Comment(3)
I cannot trigger manually, because this fuction i have to call , when another function is "onComplete "Dugger
What do you mean? You can call the function both in the live handler and elsewhere (including onComplete)Moyna
You can't just call the function if the function requires appropriate context to function; which may be common place when using .live. For simple cases though you can simply provide said context or generic context... but only for simple cases.Children
S
3

I had the same problem when using Lightbox2 which binds the click event using live(). In my case the solution was to trigger the event like this:

jQuery**('a[rel^="lightbox"]')**.ready(function () {
jQuery("#myimglink").trigger("click");
})

As you can see i was triggering the event only after all the images that are using lightbox are ready. Hope it will help you all - this being my first contribution to stackoverflow :).

Socioeconomic answered 2/9, 2011 at 20:11 Comment(0)
R
2

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?

Relent answered 30/6, 2010 at 13:53 Comment(0)
P
0

As I was facing a similar situation, I found SLaks' solution perfectly helpful. However, attaching .live("click", handlerFn) and .bind("click", handlerFn) to the same elements seems to be conflicting (I don't know what precedence rules apply in such a case). But it works fine if you use a custom event instead of "click" for the trigger.

$(".elems").bind("customEvent", handlerFn);
$(".elems").live("click", handlerFn);

$(".elems").trigger("customEvent");

function handlerFn() {
}
Poetize answered 4/3, 2011 at 13:23 Comment(0)
B
0

This is another way that works for me.

$('#test').live('click', function() {
    alert("It works!");            
});

$('#test')[0].click();

The important part here is:

$('#test')[0].click();
Bobbee answered 18/12, 2014 at 8:17 Comment(0)
O
-4

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

Octaviaoctavian answered 30/6, 2010 at 13:51 Comment(5)
Wrong. live is very different from bind. You're confusing it with livequery, which does work like that.Moyna
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"Octaviaoctavian
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.Moyna
@Moyna 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.Colonize
Ok then my apologise for that, ill have to read up about the way live works in more detail. thanks for that.Octaviaoctavian

© 2022 - 2024 — McMap. All rights reserved.