0

I am using the code below to load html into my page:

$(document).ready(function () {

   $('#loadingArea').load('includes/template1.html');

});

It works great but for some reason when I use any query to target any divs belonging to the loaded html file it won't work for example:

template1.html contains:

Button Inside the Template

If I try to use:

$("#mybutton").click(function(){

    alert('Something');        

});

It will not work.

How can I fix this?

Satch3000
  • 47,356
  • 86
  • 216
  • 346

2 Answers2

1

learn about event delegation see:

$("body").on('click','#mybutton',function(){

    alert('Something');        

});
madalinivascu
  • 32,064
  • 4
  • 39
  • 55
1

You need to delegate the events. Delegate the event by attaching to the nearest static object:

$("#loadingArea").on("click", "#mybutton", function(){
    alert('Something');
});
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252