Reputation: 12179
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
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