Goje87
Goje87

Reputation: 2869

Binding event to DOM elements which can be dynamically loaded

In my page, I'll be having few DIVs with class name 'imp'. And I might also be loading a few more DIVs with same class name. I want to bind all these DIVs with an event (may be 'load') so that I can meet the requirement of executing a function for all these DIVs irrespective of whether they are already present on the page or will be loaded later. I have tried the following but didn't have any success.

$('.imp').live('load', function()
{
  alert('encountered an important data');
});

The problem here is that 'load' event can be bound only to the element with url like images, scripts etc. Kindly let me know how can I achieve the above mentioned requirement.

Upvotes: 0

Views: 129

Answers (1)

kapa
kapa

Reputation: 78731

In your jQery Ajax call (like load), you can specify a function which will run when the call is completed and successful. Might run your function there.

Something like:

$('#result').load('ajax/test.html', function() {
  $('.imp', $('#result')).each(function (){
    alert('encountered an important data');
  });
});

Upvotes: 1

Related Questions