-3

My code doesn't show errors, but also doesn't work as it should. The output from the ajax call is working fine but I just can't get anything out of the onClick, no errors message, nothing.

The HTML

<div id="serials"></div>
<div id="accounts_info_key" style="text-align:left"></div>

The Ajax call

$(document).ready(function() { 
$('.tab2').click(function(){
        var account_id = $('#account_id').val();
              $.ajax({
                 url: 'ajax/account_info_serials_ajax.php',
                 type:'POST',
                 dataType: 'json',
                 data: 'account_id='+account_id,
                        success: function(response){
                $('#serials').html(response.table),
                $('#accounts_info_key').html(response.key),
                $('.drop_down').hide();
                },
              error:function (xhr, ajaxOptions, thrownError){
              alert(thrownError);
               } // End of success function of ajax form
              }); //end of ajax
       });//close trigger
  });//close whole function

The PHP

   $response = '';
   $response .='<div class="question">';
   $response .='plop';

   $response .='</div>';
   $response .='<div class="drop_down">';
   $response .='bob';
   $response .='</div>';

   $output_array = array(
              'table' => $response,
              'key' => $key,
              'count' => $count 
    );

  echo json_encode($output_array);

The jquery onclick

$(document).ready(function() {  
    $('#serials').on("click", ".question", function(e){
        alert('smeg');  
    });
});

Please note, I am not asking how to do it, I am asking why my version is not working, it is NOT a duplicate question

tatty27
  • 1,525
  • 4
  • 32
  • 67

2 Answers2

3

I have created a fiddle that shows how it should be done in my opinion http://jsfiddle.net/6VtA8/.

$(document).ready(function () {
    $('.tab2').click(function () {
        var account_id = $('#account_id').val();
        $.ajax({
            url: '/echo/json/',
            type: 'POST',
            dataType: 'json',
            data: {'something':'else'}
        }).done( function (response) { $('#serials').html('<div class="question ">abcd</div>'); });
    });
}); //close whole function 

$(document).ready(function () {
    $('#serials').on("click", ".question", function (e) {
        alert('smeg');
    });
});

There are multiple reasons that could cause your code to not work. It could even be the fact that your div (from some css rule) would end up having a height of 0 and therefore the event wouldn't trigger. So instead of debugging I chose to do a little rewriting.

0

You have to subscribe to the event click on #serials once it's in the dom. So when your ajax callback is over and that .question is in the dom, you subscribe to the click on #serials.

KitAndKat
  • 923
  • 3
  • 14
  • 28
  • Shouldn't need to. `#serials` exist in the DOM on `$(document).ready(function () { ... });`. The OP is using a delegated event handler so it should be fine even after the ajax loads. – War10ck Mar 11 '14 at 16:43
  • So what do I need to do that is different to make that happen?<<< ignore that – tatty27 Mar 11 '14 at 16:44