Last active
May 17, 2016 17:39
-
-
Save TheRealJAG/014f8a8fee4eadd8a070e3a9d3f9ed84 to your computer and use it in GitHub Desktop.
Append child divs to each parent
This file contains hidden or 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
<!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