Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to trigger Live() click in jquery

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 !!!!

like image 208
AlexC Avatar asked Jun 30 '10 13:06

AlexC


People also ask

How to use live function in jQuery?

jQuery live() MethodThe live() method attaches one or more event handlers for selected elements, and specifies a function to run when the events occur. Event handlers attached using the live() method will work for both current and FUTURE elements matching the selector (like a new element created by a script).

What is trigger method in jQuery?

jQuery trigger() Method The trigger() method triggers the specified event and the default behavior of an event (like form submission) for the selected elements. This method is similar to the triggerHandler() method, except that triggerHandler() does not trigger the default behavior of the event.

How do you call one event from another event in jQuery?

$('#b1'). on('click', function(){ $('#select_item'). trigger('change'); }); $('#select_item'). on('change', function(){ var jbyr = $(this).

Which jQuery method triggers or binds a function to the error event of selected elements?

jQuery error() Method The error() method triggers the error event, or attaches a function to run when an error event occurs.


2 Answers

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.

like image 170
SLaks Avatar answered Nov 15 '22 22:11

SLaks


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 :).

like image 40
Grati Avatar answered Nov 15 '22 21:11

Grati