I'm trying to write a generic javascript script to facilitate clicking through <a href>
links while only replacing the inner HTML instead of reloading the whole page. The strange thing is, it works, except on any link inside of freshly loaded HTML.
<script src="{{ asset('bundles/app/js/jquery-2.2.0.min.js') }}"></script>
<script src="{{ asset('bundles/app/js/jquery.menu-aim.js') }}"></script>
<script src="{{ asset('bundles/app/js/main.js') }}"></script>
<script type="text/javascript">
$(document).ready(function() {
$("a").on("click", function(){
event.preventDefault();
$.ajax({
'url': $(this).attr('href'),
type: "post",
success: function(response, status) {
document.getElementById("content").innerHTML = response;
},
error: function() {
console.log('failure');
}
});
});
});
</script>
When I place the exact same URL from the loaded HTML directly in the sidebar menu containing the initial links, it loads fine. According to the documentation, the .on function should attach itself to any elements added later. I've also tried .delegate and the deprecated .live as suggested by older answers but then even the menu sidebar stopped working.
What am I missing here?