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
  • 44,076
  • 86
  • 209
  • 342

2 Answers2

1

learn about event delegation see:

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

    alert('Something');        

});
madalinivascu
  • 31,556
  • 4
  • 35
  • 52
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
  • 160,666
  • 24
  • 190
  • 242