mg1075
mg1075

Reputation: 18155

how to use jquery .delegate() with wrapped function on ajaxed content?

Suppose you have a page called /start.html, and in that page you have a wrapped function that gets called when the body loads.

function eatApples() {  
 // some code  
}  
function eatOranges() {  
// some code  
}  
function eatFruit() {  
$(eatApples());
$(eatOranges()); 
}  
// call the eatFruit function on page load
$(eatFruit()); 

Now suppose you also have a page called /end.html, and this page contains ajaxed content. Furthermore, you want to call the eatFruit() on that ajaxed content.

Is this possible with jQuery's delegate or live functions? http://api.jquery.com/delegate/

Upvotes: 2

Views: 904

Answers (1)

Gabriele Petrioli
Gabriele Petrioli

Reputation: 196306

First of all you code should be

function eatApples() {  
 // some code  
}  
function eatOranges() {  
// some code  
}  
function eatFruit() {  
  eatApples();
  eatOranges(); 
}  
// call the eatFruit function on page load
$(eatFruit);

unless the eatApples and eatOranges return functions as a result.

Now if the end.html is being called with ajax, jquery allows for callback handlers to be executed when the ajax content is loaded.

The delegate and live function is use to control events that occur from user interaction.

If the new content meets the criteria specified when using the delegate or live methods then yes, it will work with the ajax loaded content.

For example, if you set a

$('.aclass').live('click',function(){
   alert('clicked');
});

then if the content loaded from ajax have an element with class aclass it will also fire the alert when clicked.

If you could provide more info you will get a more detailed answer.

Upvotes: 2

Related Questions