I have a master page that loads content into a div based on which link is clicked. I do this by using jQuery to add a click handler for certain anchors:
<div id="nav">
<a href="somepage.html">
...
</div>
<div id="content"/>
<script type="text/javascript">
$(document).ready(function() {
$('#nav a, #footer a').click(function(e) {
$('#content').load($(this).attr('href'));
return false;
});
});
</script>
Some of the pages that get loaded into #content also have anchors for which I want to add a click handler (so the result of those clicks also go into #content). I’ve tried this:
$('#content a').click(function(e) {
...
});
but that doesn’t appear to work. Any ideas how I can set the click handler on anchors in those loaded pages?
Should work. When you add elements dynaically you need to use
on()to bind them to an event. From the docs:Ideally you’d want a parent element closer in the DOM than
document.