el_pup_le
el_pup_le

Reputation: 12179

Execute live function

I've got an ajax call I want to execute for 2 events, whenever the document is loaded and whenever foo-live-event is clicked. What is the best way to do this?

$(document).delegate(".foo-live-element", "click", function() {
  $.ajax({
    //ajax call
  });
});

Upvotes: 0

Views: 52

Answers (1)

Jason
Jason

Reputation: 15931

$(document).ready(function() {
  $(".foo-live-element").live("click", function() {
     $.ajax({..});
  });

   /* the following code will fire the click event on all foo-live-element element's after the page loads */
   $(".foo-live-element").each(function(index, item) {
     $(item).click();
   });
});

this code will fire whenever an element with class="foo-live-element" is clicked, even if that element is created dynamically.

Upvotes: 1

Related Questions