-1

I have a page with an empty div and when the user clicks on a button, a jQuery function is executed and loads some content. The problem I have is that I can't access the elements inside the div.

For example, if a link is loaded, if I have a function that executes when a link is clicked, it has no effect since the link is loaded content.

EDIT: an example code of how I load content.

$('#all').click(function(e){
    e.preventDefault();
    all = true;
    $.ajax({
        type: 'POST',
        url: './?ajax=cars',
        data : {all : true},
        success: function(data){
            $('#ajaxTable').html(data);
        }
    });
});
Paolo
  • 351
  • 2
  • 4
  • 12

1 Answers1

4

Replace:

$('#all').click(function(e){ /* code here */ });

With:

$(document).on('click', '#all', function(e){ /* code here */ });

The #all element is created after the click event is assigned so it won't apply to a new #all element. Using $(document).on() any #all contained inside the document regardless of load time will observe the event.

MaKR
  • 1,882
  • 2
  • 17
  • 29