Skip to content

Instantly share code, notes, and snippets.

@TheRealJAG
Last active May 17, 2016 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheRealJAG/014f8a8fee4eadd8a070e3a9d3f9ed84 to your computer and use it in GitHub Desktop.
Save TheRealJAG/014f8a8fee4eadd8a070e3a9d3f9ed84 to your computer and use it in GitHub Desktop.
Append child divs to each parent
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Widgets</title>
<script src="https://code.jquery.com/jquery-1.11.0.js"></script>
<script type="text/javascript">
<!--
$(document).ready(function() {
appendChildren();
});
function appendChildren() {
var allDivs = document.getElementsByTagName("div");
var allDivsLength = document.getElementsByTagName("div").length;
for (var i = 0; i < allDivsLength; i++) {
var newDiv = document.createElement("div");
decorateDiv(newDiv, allDivs[i].id);
allDivs[i].appendChild(newDiv);
}
}
// Mock of decorateDiv function for testing purposes
function decorateDiv(div, parent) {
$( div ).append( "<p>This is the child element of "+parent+"</p>" );
}
-->
</script>
</head>
<body>
<div id="a"> DIV A
<div id="b"> DIV B </div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment