Skip to content

Instantly share code, notes, and snippets.

@awestmoreland
Created August 4, 2015 15:23
Show Gist options
  • Save awestmoreland/0d9616f0b6ca8e858e39 to your computer and use it in GitHub Desktop.
Save awestmoreland/0d9616f0b6ca8e858e39 to your computer and use it in GitHub Desktop.
OVdPoo
<input type="button" id="addChild" value="Add child">
<hr>
<div id="parent">
</div>
<hr>
<div id="total">Total children: <span></span></div>
$(document).ready(function() {
countChildren();
$('#addChild').on('click',function(){
addChild();
});
// Load clearly isn't going to work, but how else do
// I detect a new element added to the page?
$('#parent').on('load','.child', function() {
countChildren();
});
});
function countChildren(){
$('#total span').text($('#parent .child').length);
}
function addChild(){
$('<div class="child">Child</div>').appendTo('#parent');
}
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment