I've seen this problem posted on Stack Overflow couple of times, but the solution I found is usually very old and talks about delegate
method which is deprecated.
I am looking for a solution for jQuery 3.x. This is my AJAX call:
$.ajax({
type: "get",
url: "/content",
data: {
name: {{ name }}
},
success: function(data) {
$("#appendsection").append(data);
}
});
Here I am appending the success data to a div called appendsection
This appendsection
div does not execute any javascript, it executes CSS, but no javascript and no jquery. I see this problem most of the time, any data I get from ajax call does not want to execute javascript.
This is the appended content
<div class="sortingbuttons">
<select id="sort">
Sort By
<option value="name">Relevance</option>
<option value="price">Cheapest</option>
<option value="recommend">Recommended</option>
</select>
</div>
The #sort
id is executed on app.js
$('#sort').change(function(){
$('.section').hide();
$('#' + $(this).val()).show();
});
So, the sorting does not work on the appended content, so the javascript is not working on the appended content.