-1

Probably a silly question, but still new to JQuery.

I have a div acting as a button to load in content via .load, problem is anything I load in JS will not work on it.

HTML:

<div class="a" id="aa">Button #1</div>
</br>
</br>
<div id="loada"></div>
</br>
</br>
<div id="loadb"></div>

JS:

$(document).ready(function(){
  $('#aa').click(function(){
    $('#loada').load('load.htm #bb');
});
  $('#bb').click(function(){
    $('#loadn').load('load.htm #cc');
});

});

Load HTM:

<div class="b" id="bb">Button #2</div>
<div class="c" id="cc">Content #1</div>

I understand that you need to use .on for already loaded JS to work on dynamically created HTML, however I can't seem to get it right.

Anyone able to help?

Edit: What is wrong with the below?

$('#bb').on('click', '#bb', function(event){
  $('#loadb').load('load.htm #cc');
});
NuMs
  • 139
  • 3
  • 15

3 Answers3

1
$("#selector").on("click", function(event){
});

delegated-events approach attaches an event handler to only one elemen

 $("#selector").on("click", "selector", function(event){

    });
PSR
  • 39,804
  • 41
  • 111
  • 151
0

you can try make click on callback function of load like this:

$(document).ready(function(){
  $('#aa').click(function(){
    $('#loada').load('load.htm #bb',function(){
        $('#bb').click(function(){
            $('#loadb').load('load.htm #cc');
        });
    });
});
Mohamed AbdElRazek
  • 1,654
  • 14
  • 17
-1
    $(document).ready(function(){
      $('#aa').on("click",function(){
        $('#loada').load('load.htm #bb');
           $('#bb').on("click",function(){
                   $('#loadn').load('load.htm #cc');
                 });
          });
    });

if you are using jQuery 1.9 use "on" , if using older version use "live". [live is deprecated remember.]
A.T.
  • 24,694
  • 8
  • 47
  • 65