A Pen by Andy Westmoreland on CodePen.
Created
August 4, 2015 15:23
-
-
Save awestmoreland/0d9616f0b6ca8e858e39 to your computer and use it in GitHub Desktop.
OVdPoo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<input type="button" id="addChild" value="Add child"> | |
<hr> | |
<div id="parent"> | |
</div> | |
<hr> | |
<div id="total">Total children: <span></span></div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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